cloneNode в интернет-проводнике - PullRequest
       1

cloneNode в интернет-проводнике

7 голосов
/ 07 сентября 2010

При выполнении следующего кода IE выдает ошибку - Object не поддерживает это свойство или метод - ссылаясь на метод cloneNode (). «i» - счетчик цикла, источник и dest - оба элемента выбора HTML.

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

FF и Chrome ведут себя как положено. Любые идеи о том, как заставить IE выполнять cloneNode ()? Отладчик IE 8 показывает, что source.options [i] имеет метод cloneNode ().

Спасибо.

Ответы [ 4 ]

9 голосов
/ 07 сентября 2010

IE требует

new Option()

конструкция.

document.createElement( 'option' );

или

cloneNode()

потерпит неудачу. Конечно, все параметры работают должным образом в соответствующем веб-браузере.

5 голосов
/ 10 сентября 2010

На самом деле, 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 ));
1 голос
/ 22 мая 2013

Я нашел этот пост полезным: IE cloneNode на самом деле не клонирует!

0 голосов
/ 07 сентября 2010
<!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>
...