在工作中遇到了这样的情况,当我在一个iframe的js中声明了一个CodeMirror.fromTextArea对象,但是该对象在父html中才需要绑定事件,所以记录下来,以备后用:
直接上代码:
1.在iframe的js中的代码:
//初始化源码区
$("#text").val($.ajax({url:"components/frame.html",async:false}).responseText);
var codeEditor = CodeMirror.fromTextArea($("#text")[0],{ mode:"text/html",lineNumbers:true,tabMode:"indent",lineWrapping:false,height:"100%", width:"100%"});
2.在父html中代码:
var ObjEditor=$(obj)[0].contentWindow.codeEditor;
//绑定改变事件
ObjEditor.setOption('onChange',function(ObjEditor){
editorChange(ObjEditor);
});
就这样,父html中就将iframe中的CodeMirror对象绑定了改变事件。
如果是在iframe的js中直接声明直接绑定则为:
//初始化源码区
$("#text").val($.ajax({url:"components/frame.html",async:false}).responseText);
var codeEditor = CodeMirror.fromTextArea($("#text")[0],{ mode:"text/html",lineNumbers:true,tabMode:"indent",lineWrapping:false,height:"100%", width:"100%",onChange:function(n){operator(n);}});