简体   繁体   English

append / appendChild不适用于所有项目

[英]append / appendChild not working on all items

I'm trying to move all the list items from an list to another using only javascript but for some reason only half of them are actually moved. 我正在尝试仅使用javascript将所有列表项从列表移动到另一个列表但由于某种原因,其中只有一半实际上被移动了。

Heres a working example of what I'm doing: 以下是我正在做的事情的一个实例:

 var results_ul = document.getElementById('results'); var stores_li = document.getElementsByClassName('store-list-item'); for (var x = 0; x < stores_li.length; x++) { document.getElementById('hide').appendChild(stores_li[x]); stores_li[x].className += ' teste'; } 
 <p>results</p> <ul id="results"> <li class="store-list-item">Teste 1</li> <li class="store-list-item">Teste 2</li> <li class="store-list-item">Teste 3</li> <li class="store-list-item">Teste 4</li> </ul> <p>Hide:</p> <ul id="hide"></ul> 

What seems to be the problem? 什么似乎是问题?

getElementsByClassName returns a live list. getElementsByClassName返回实时列表。

When you append the element to a different element, you change its position in the list. 将元素追加到其他元素时,可以更改其在列表中的位置。

So it starts off as: 所以它开始于:

1 2 3 4

Then you move the first one: 然后你移动第一个:

2 3 4 1

Then you access the second one … but the second one is now 3 because everything has shuffled down the list. 然后你访问第二个...但第二个现在是3因为一切都在列表中乱窜。


You could copy each element into an array (which will not be a live list) and then iterate over that array to move them (so they won't change positions as you go). 您可以将每个元素复制到一个数组(它不是实时列表),然后遍历该数组以移动它们(这样它们就不会改变位置)。

Alternatively, you could use querySelectorAll which returns a non-live list. 或者,您可以使用querySelectorAll返回非实时列表。

You should better use querySelectorAll than getElementsByClassName 您最好使用querySelectorAll不是getElementsByClassName

 var results_ul = document.getElementById('results'); var stores_li = document.querySelectorAll('.store-list-item'); stores_li.forEach((item)=>{ document.getElementById('hide').appendChild(item); item.className += ' teste'; }); 
 <p>results</p> <ul id="results"> <li class="store-list-item">Teste 1</li> <li class="store-list-item">Teste 2</li> <li class="store-list-item">Teste 3</li> <li class="store-list-item">Teste 4</li> </ul> <p>Hide:</p> <ul id="hide"></ul> 

Try use querySelectorAll . 尝试使用querySelectorAll It'll returns a non-live list. 它将返回一个非实时列表。 That's what you need. 这就是你所需要的。

var stores_li = document.querySelectorAll('.store-list-item');

To increase more information: 要增加更多信息:

Live : when the changes in the DOM are reflected in the collection. 直播 :当DOM中的更改反映在集合中时。 The content suffers the change when a node is modified. 修改节点时,内容会受到影响。

Non-Live : when any change in the DOM does not affect the content of the collection. 非实时 :DOM中的任何更改不会影响集合的内容。

document.getElementsByClassName() is an HTMLCollection, and is live. document.getElementsByClassName()是一个HTMLCollection,并且是实时的。

document.querySelectorAll() is a NodeList and is not live. document.querySelectorAll()是一个NodeList,不是实时的。

In your code you are removing each element from the first list and inserting into the new list. 在您的代码中,您将从第一个列表中删除每个元素并插入新列表。 After you remove 2 elements it will have only 2 elements in the first list but now you are searching the 3 rd index in the loop which is not there. 删除2个元素后,它将在第一个列表中只有2个元素,但现在您正在循环中搜索不存在的第3个索引。 So to make it work i have prepended each element from the last. 所以为了使它工作,我已经预先添加了每个元素。

 var results_ul = document.getElementById('results'); var stores_li = document.getElementsByClassName('store-list-item'); var hide_ul = document.getElementById('hide'); for (var x = 0, y = stores_li.length; x < y; x++) { hide_ul.insertBefore(stores_li[yx-1],hide_ul.firstChild); stores_li[x].className += ' teste'; } 
 <p>results</p> <ul id="results"> <li class="store-list-item">Teste 1</li> <li class="store-list-item">Teste 2</li> <li class="store-list-item">Teste 3</li> <li class="store-list-item">Teste 4</li> </ul> <p>Hide:</p> <ul id="hide"></ul> 

Or you may want to clone the element with Jquery and you can push into the clonned ones then delete the orginals from top. 或者你可能想用Jquery克隆元素,你可以推入克隆元素,然后从顶部删除orginals。 I could not find any equivalent of clone() for js but if you want to check link is here 我找不到js的任何克隆()的等价物,但如果你想检查链接在这里

var results_ul = document.getElementById('results');
var stores_li = document.getElementsByClassName('store-list-item');

while(stores_li.length>0) {
   document.getElementById('hide').appendChild(stores_li[0]);
   stores_li[x].className += ' teste';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM