我对CodeMirror和JavaScript都比较陌生,所以我试图同时使用它们来更好地了解它们。
我以前有代码,当我编辑/执行代码时,它可以工作并显示正确的输出。而执行时以及在Chrome中进行检查时,底部什么也不做,它显示标题中提到的错误。
为了尝试更好地解释,这是一些代码:
此作品
index.html
<textarea id="editor" name="editor" style="display: none;">
Some code
</textarea>
...
var editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
lineNumbers: true,
mode: 'text/html',
autoCloseTags: true
});
executeCode();
executeCode()函数
function executeCode(){
var text = editor.getValue();
var ifr = document.createElement("iframe");
ifr.setAttribute("frameborder", "0");
ifr.setAttribute("id", "iframeOutput");
document.getElementById("iframewrapper").innerHTML = "";
document.getElementById("iframewrapper").appendChild(ifr);
var ifrw = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
ifrw.document.open();
ifrw.document.write(text);
ifrw.document.close();
};
这不是
index.html(没有在文本区域中键入代码-executeCode()相同)
$(function(){
$.get('example.html', function(data){
$('textarea#editor').val(data);
// now init codemirror
var a = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'text/html',
tabMode: 'indent',
lineNumbers: true,
lineWrapping: true,
autoCloseTags: true
});
executeCode();
})
})
[就像我说的那样,我对JS和CodeMirror都是新手,所以很可能我很想念我,但是如果您能指出我出了问题的地方,我将不胜感激。
提前感谢!
因此,您面临的最可能的问题是,您试图使用CodeMirror 2方法检索编辑器的内容时正在加载CodeMirror 1库。
[CodeMirror 1使用getCode()method检索编辑器的内容
var code = editor.getCode(); //a string with your editor content.
[CodeMirror 2改用getValue()method,
var code = editor.getValue(); //a string with your editor content.