CodeMirror:iframe中的Javascript无法正常工作

时间:2018-01-29 06:58:19

标签: javascript iframe codemirror

我正在尝试构建代码播放器,显示包含iframe中脚本的html页面的结果。但这里似乎有些不对劲。似乎javascript不工作每次我点击按钮我看到控制台中的错误。

<!DOCTYPE html>
 <html>
     <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link rel="stylesheet" href="codemirror/lib/codemirror.css">
          <link rel="stylesheet" href="codemirror/theme/solarized.css">

          <script src="codemirror/lib/codemirror.js"></script>
          <script src="codemirror/mode/xml/xml.js"></script>
          <script src="codemirror/mode/htmlmixed/htmlmixed.js"></script>

    </head> <style> .CodeMirror{border:1px solid black} </style>
     <body>
            <form>
         <div id="editorContainer" style="width:50%">
           <textarea id="code" name="code" class="CodeMirror">

          <button id="btn" onclick="say()">hello</button>
            <script>

                function say(){
                   alert('test')
                 }
         </script>



           </textarea>
         </div>
         </form>
         <button>click</button>
         <iframe id="frame"></iframe>
            <script type="text/javascript">
            var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
                mode: "text/html",
                lineNumbers: true
              });
              $("button").click(function(){

                  var code= editor.getValue();
                  $("iframe").contents().find("html").html(code);
              })
      </script>

  </body>
  </html

&GT;

0 个答案:

没有答案