【问题标题】:cloneNode in internet explorerInternet Explorer 中的克隆节点
【发布时间】:2013-05-22 17:57:38
【问题描述】:

执行以下代码时,IE 会抛出错误——对象不支持此属性或方法——指的是 cloneNode() 方法。 'i' 是循环计数器,source 和 dest 都是 HTML 选择元素。

dest.options[dest.options.length] = source.options[i].cloneNode( true );

FF 和 Chrome 的行为符合预期。关于如何让 IE 执行 cloneNode() 的任何想法? IE 8 调试器显示 source.options[i] 确实有一个 cloneNode() 方法。

谢谢。

【问题讨论】:

  • 显示循环的代码,我怀疑循环开始或结束的 source.options[i] 不是 DOM 元素。

标签: javascript internet-explorer clonenode


【解决方案1】:

IE 需要

new Option()

构造。

document.createElement( 'option' );

cloneNode()

会失败。当然,所有选项都可以在适当的网络浏览器中按预期工作。

【讨论】:

    【解决方案2】:

    实际上,cloneNode 并没有抛出任何错误。将您的代码分解成更小的块以正确识别错误的来源:

    var origOpt = source.options[i];
    var clonedOpt = origOpt.cloneNode( true );  // no error here
    var destOptLength = dest.options.length;
    dest.options[destOptLength] = clonedOpt;    // error!
    dest.options.add(clonedOpt);                // this errors too!
    
    dest.appendChild(clonedOpt);                // but this works!
    

    或者,把它放回原来的样子,全部放在一行上:

    dest.appendChild(source.options[i].cloneNode( true ));
    

    【讨论】:

      【解决方案3】:

      我发现这篇文章很有用:IE’s cloneNode doesn’t actually clone!

      【讨论】:

        【解决方案4】:
        <!doctype html>
        <html lang="en">
        <head>
        <meta charset= "utf-8">
        <title>Untitled Document</title>
        <style>
        p, select,option{font-size:20px;max-width:640px}
        </style>
        <script>
        
        function testSelect(n, where){
            var pa= document.getElementsByName('testselect')[0];
            if(!pa){
                pa= document.createElement('select');
                where.appendChild(pa);
                pa.name= 'testselect';
                pa.size= '1';
            }
            while(pa.options.length<n){
                var i= pa.options.length;
                var oi= document.createElement('option');
                pa.appendChild(oi);
                oi.value= 100*(i+1)+'';
                oi.text= oi.value;
            }
            pa.selectedIndex= 0;
            pa.onchange= function(e){
                e= window.event? event.srcElement: e.target;
                var val= e.options[e.selectedIndex];
                alert(val.text);
            }
            return pa;
        }
        window.onload= function(){
            var pa= testSelect(10, document.getElementsByTagName('h2')[0]);
            var ox= pa.options[0];
            pa.appendChild(ox.cloneNode(true))
        }
        
        </script>
        </head>
        
        <body>
        <h2>Dynamic Select:</h2>
        <p>You need to insert the select into the document, 
        and the option into the select,
        before IE grants the options any attributes.
        This bit creates a select element and 10 options,
        and then clones and appends the first option to the end.
        <br>It works in most browsers.
        </p>
        </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-19
          • 2015-12-18
          • 2010-10-12
          • 2017-05-07
          • 1970-01-01
          相关资源
          最近更新 更多