代码镜像预览-不在框架中

Codemirror preview - not in a iframe?

本文关键字:框架 镜像 代码      更新时间:2023-09-26

我正在尝试做与本例完全相同的事情,只是,代码不应该在iframe中打印,而是在div中打印。

http://codemirror.net/demo/preview.html

这个不工作…我需要一个不同的方法。

$("#codeMirrorTextarea").keyup(function () {
     $("#div").html($(this).val());
});

希望你能帮忙!

使用普通JS:

t = document.getElementById('code');
t.addEventListener('input',function(){
    document.getElementById('result').innerHTML = t.value;
});

使用oninput事件处理程序增加了对非键盘设备的支持,以及这里指出的

演示

编辑:代码使用CodeMirror:

$(function () {
    $("textarea").each(function (i) {
        editor = CodeMirror.fromTextArea(this, {
             lineNumbers: true
        });
    });
});
document.getElementById('result').innerHTML=editor.getValue();

演示更新的演示,也更新代码。

editor.getValue()已在您提供的示例链接中使用。

CodeMirror使用getValue()和更多方法的API