Codemirror预览 - 不在iframe中?

时间:2011-10-25 14:56:31

标签: javascript jquery html codemirror

我尝试的方法与此示例完全相同,只是代码不应该在iframe中打印,而是在div中。

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

这不起作用......我需要一种不同的方法。

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

希望你能帮忙!

1 个答案:

答案 0 :(得分:3)

使用普通香草JS:

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

使用oninput事件处理程序还可以添加对非键盘设备的支持as pointed out here

Demo

编辑: 使用CodeMirror的代码:

$(function () {
    $("textarea").each(function (i) {
        editor = CodeMirror.fromTextArea(this, {
             lineNumbers: true
        });
    });
});

document.getElementById('result').innerHTML=editor.getValue();

Demo Updated demo也会更新代码。

editor.getValue()已在您提供的example link中使用。

CodeMirror's API for usage of getValue() and more methods