Codemirror和iFrame问题

时间:2012-05-08 13:37:51

标签: javascript jquery html css iframe

我在我的一个项目中使用Codemirror。 代码如下:

<script>
window.onload = function() {
var te = document.getElementById("js");
var te_html = document.getElementById("html");
var te_css = document.getElementById("css");

window.editor = CodeMirror.fromTextArea(te, {
mode: "javascript",
    lineWrapping: true,
    });

 window.editor_css = CodeMirror.fromTextArea(te_css, {
    mode: "css",
    lineWrapping: true,
    });

  window.editor_html = CodeMirror.fromTextArea(te_html, {
    mode: "text/html",
    lineWrapping: true,
  })
};
</script>

我也在使用Jeffrey Way的“如何将自定义HTML和CSS注入iframe”脚本来构建类似于Tinkerbin或JSFiddle的东西。

这是我的代码:

<script type="text/javascript">
(function() {
    $('.grid').height( $(window).height() );    

    var contents = $('iframe').contents(),
    body = contents.find('body'),
    styleTag = $('<style></style>').appendTo(contents.find('head'));

    $('textarea').keyup(function() {
        var $this = $(this);
        if ( $this.attr('id') === 'html') 
        {
        body.html( $this.val() );
        } 
        else 
        {
        styleTag.html( $this.val() );
        }

    });

})();
</script>

一切都很好,除了激活CodeMirror脚本后,RESULT选项卡中不显示任何内容。如果我删除CodeMirror代码部分,它可以正常工作。

这是我的项目链接:

http://mediodesign.ca/cms-test/

有谁知道冲突在哪里?

谢谢!

1 个答案:

答案 0 :(得分:0)

确保已加载所有javascripts

mode/javascript.js
mode/css.js
mode/xml.js

如果您在xhtml文件中使用此脚本。使用单引号(')而不是双引号("

离。

<script>
window.onload = function() {
var te = document.getElementById('js');
var te_html = document.getElementById('html');
var te_css = document.getElementById('css');

window.editor = CodeMirror.fromTextArea(te, {
mode: 'javascript',
    lineWrapping: true,
    });

 window.editor_css = CodeMirror.fromTextArea(te_css, {
    mode: 'css',
    lineWrapping: true,
    });

  window.editor_html = CodeMirror.fromTextArea(te_html, {
    mode: 'text/html',
    lineWrapping: true,
  })
};
</script>

Jeffrey Way的如何将自定义HTML和CSS注入iframe ...与加载codemirror脚本无关

相关问题