代码镜像和 iFrame 问题

Codemirror and iFrame issue

本文关键字:问题 iFrame 镜像 代码      更新时间:2023-09-26

我正在我的一个项目中使用 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 脚本后,"结果"选项卡中不会显示任何内容。如果我删除代码镜像代码部分,它可以正常工作。

这是我的项目链接:

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

有人知道冲突在哪里吗?

谢谢!

确保你已经加载了所有的javascript。

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》...与加载代码镜像脚本无关