大家早上好。
我经常看到网上一些JS代码放在一个textarea文本域里,下面有一个按钮,叫“运行代码”,点击之后,就可以看到效果了,
请问这是怎么实现的呢。可能我表达的不是很好,大家明白我说的吗,

解决方案 »

  1.   

    <html>
    <head>
    <title>test</title>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    <script type="text/javascript">
    <!--
    var pop=function () {
    var sor = document.getElementById("tt").value;
    var pp = window.open("","","");
    pp.opener = null;
    pp.document.write(sor);
    pp.document.close();
    };
    //-->
    </script>
    </head>
    <body>
    <textarea id="tt" rows="" cols=""><script>alert("open")</script></textarea><input type="button" value="" onclick="pop()" />
    </body>
    </html>
      

  2.   


    好厉害啊,你能不能再帮我一下。
    比如把上面的代码改成var pp = window.open("b.html","","");
    b.html里面包含了<html><body></body></html>
    我想把文本框里面的内容放在b.html里面的body里面,
    用document.write 的话,会把b.html里面的东西覆盖掉了。
      

  3.   

    document.write的时候,你把html那些代码加上不就好了。也用不上b那个页面的 pp.document.write("<html><body>"+sor+"</body></html>");
      

  4.   

    function runCode(objId){  //定义一个运行代码的函数,
      var obj =document.getElementById(objId);
      var code=obj.value;//即要运行的代码。
      var newwin=window.open('','','');  //打开一个窗口并赋给变量newwin。
      newwin.opener = null // 防止代码对论谈页面修改
      newwin.document.write(code);  //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
      newwin.document.close();
    }