Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Internet Explorer:将子项复制/克隆到阵列_Javascript_Jquery_Internet Explorer_Dom - Fatal编程技术网

Javascript Internet Explorer:将子项复制/克隆到阵列

Javascript Internet Explorer:将子项复制/克隆到阵列,javascript,jquery,internet-explorer,dom,Javascript,Jquery,Internet Explorer,Dom,从昨天起我就一直在玩弄这个问题。我似乎找不到解决办法 问题: <ul> <li>Cats</li> <li>Dogs</li> <li>Bats</li> <li>Ogre</li> </ul> 在Internet Explorer 8中的$(document).ready()上,数组将包含li元素,但不包含其内部HTML 就像 <li></li> &

从昨天起我就一直在玩弄这个问题。我似乎找不到解决办法

问题:

<ul>
<li>Cats</li>
<li>Dogs</li>
<li>Bats</li>
<li>Ogre</li>
</ul>
在Internet Explorer 8中的$(document).ready()上,数组将包含li元素,但不包含其内部HTML

就像

<li></li>
<li></li>
...
上面的代码在chrome和firefox上运行得非常好。每次调用myfunc时,新的无序列表将只包含猫和狗

然而,在IE上,似乎用$(“ul”).html()更改无序列表也会更改someArray。它在第一次调用时工作,但在第二次调用时,它将从当前UL中提取元素列表

原创元素:猫、狗、蝙蝠、, 食人魔第一次呼叫IE:猫、狗、, 对IE的第二次调用:空数组

我确实尝试用$.extend克隆阵列,但似乎没有任何帮助。有没有一种方法可以保存原始UL中的子节点列表而不被IE更改


当我将子节点保存到数组中时,IE似乎使用指针引用,但不清楚您实际要做什么,但是如果您的目标是将这些元素的内容放入数组中,那么请清除列表,下面是一种方法:

// Grab the animals
var animals = [];
$("ul:first").children().each(function() {
  animals.push(this.innerHTML);
});

// Wipe out the list
$("ul").html("");

// Show the result
var index;
for (index = 0; index < animals.length; ++index) {
  display("animals[" + index + "] = '" + animals[index] + "'");
}

以一种更简单的方式:

var animals = $("ul").children().map(function(){
      return $(this).text();
}).get();

alert(animals);


我认为您的问题在于您的原始代码指向原始列表,而您正在其他地方更改它。

这是什么语言?因为包含索引0和n-2元素的
result=someArray
不是JavaScript。只是解释而已。。不应该是一个正确的代码。。这与将someArray从0迭代到n-2相同,因为n是数组的长度。对于(i=0;i// Grab the animals var animals = []; $("ul:first").children().each(function() { animals.push(this.innerHTML); }); // Wipe out the list $("ul").html(""); // Show the result var index; for (index = 0; index < animals.length; ++index) { display("animals[" + index + "] = '" + animals[index] + "'"); }
// Grab the animal elements
var animals = [];
$("ul:first").children().each(function() {
  animals.push(this.cloneNode(true));
});

// Wipe out the list
$("ul").html("");

// Show the result
var index;
for (index = 0; index < animals.length; ++index) {
  display("animals[" + index + "] = '" + animals[index].innerHTML + "'");
}
var animals = $("ul").children().map(function(){
      return $(this).text();
}).get();

alert(animals);