在实时代码编辑器中插入脚本标记的HTML Javascript问题

时间:2019-12-16 21:43:27

标签: javascript ace-editor

几天前,我使用ace 1.2.9库制作了实时代码编辑器。 在为我的网站运行几个指南时,我没有任何问题。 现在,我试图创建这个非常简单和基本的示例。 但是,当我尝试在文本区域(您想在其中插入指南的文本)中编写文本时,工作室代码编译器会报错,并且不会加载。 问题是当我添加以下行时:(它在文本区域中,不应影响编辑器代码:

document.getElementById("test").innerHTML = "Hello JavaScript";

代码:

<body onload="ready()">
    <div id="container">
      <div id="editor"></div>
      <iframe id="iframe" frameborder="0"> </iframe>
    </div>
    <script>
      function update() {
        var idoc = document.getElementById("iframe").contentWindow.document;
        idoc.open();
        idoc.write(editor.getValue());
        idoc.close();
      }
      function setupEditor() {
        window.editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.getSession().setMode("ace/mode/html");
        editor.setValue(

  `  
     <!-- You can test freely     
       with our live code editor. -->

       <html>
<body>

  <h2>Free code wiki</h2>

<p id="test"></p>

<script>
document.getElementById("test").innerHTML = "Hello JavaScript";
</script>

</body>
</html>



`,
          1
        ); //1 = moves cursor to end

        editor.getSession().on("change", function() {
          update();
        });

        editor.focus();

        editor.setOptions({
          fontSize: "16pt",
          showLineNumbers: false,
          showGutter: false,
          vScrollBarAlwaysVisible: true,
          enableBasicAutocompletion: false,
          enableLiveAutocompletion: false
        });

        editor.setShowPrintMargin(false);
        editor.setBehavioursEnabled(false);
      }

      function ready() {
        setupEditor();
        update();
      }
    </script>
  </body>
</html>

错误:

Unterminated template literal

2 个答案:

答案 0 :(得分:1)

由于<script>标记位于字符串中,因此必须对其进行转义

例如

<script>
...
<\/script

答案 1 :(得分:0)

您有很多语法错误,有3个标签标记缺少结束标记。

您有2个结束标签。

您从何处复制此代码?

相关问题