For predictable results, paste only well-formed HTML text that fits within the given text range.

解决方案 »

  1.   

    改成这样试试
    var url='<script>alert(\&quot;ok\&quot;);</script\>';
    我的浏览器竟然不能用pasteHTML,奇怪~~~
      

  2.   


    var url='<script>alert("ok");</script\>';---》var url='<script>alert("ok");</'+'script>';parseHTML不能这么做
      

  3.   

    to eglic(圪圪) (错过的,还能找回来么?) :
    var url='<script>alert("ok");</'+'script>';
    不行。期待中
      

  4.   

    不执行的原因:<script>标签只在浏览器第一次文档加载中被解析.
    但是可以用创建iframe的办法来实现,去掉前面的pasteHTML吧..如果真要那种方法,你可以把下面的代码再改改就是了.只要保证url代码在iframe中输出就OK了.
    完整的代码:
    /*    var tmp = document.selection.createRange( );
        var newHTML = "<span class='newSpan'>" + tmp.text + "</span>";
        tmp.pasteHTML(newHTML);*/
    var tmp = document.selection.createRange();
    var url="<script>alert(\'ok啦!你选择的文字是不是"+tmp.text+"\啊?')<\/script\>";
    //tmp.pasteHTML(url);
    var jsCode = url;
    var jsIframe = document.createElement("iframe");
    jsIframe.style.display = "none";//把jsIframe隐藏起来
    document.body.appendChild(jsIframe);
    with(window.frames[window.frames.length - 1]){
    document.open();
    document.write(jsCode); //执行JS代码
    document.close();
    }
    jsIframe.outerHTML="";//执行后删除iframe对象
    }
      

  5.   

    <script defer>  添加 defer 属性