简体   繁体   English

无法删除所有子节点

[英]Can't remove all childNodes

I want to remove all childNodes with class="child" from their parentNode, but only odd sequence is removed.我想从其父节点中删除所有带有class="child" childNode,但只删除了奇数序列。 How can I remove all elements with class="child" in native javascript and why only odd sequence is removed?如何删除原生 javascript 中class="child"所有元素,为什么只删除奇数序列?

 var child = document.getElementsByClassName("child"); function remove(){ for (var i=0; i<child.length; i++) { child[i].parentNode.removeChild(child[i]); } } document.getElementById("del").onclick = remove;
 <div> <div class="child"> Child 1 </div> <div class="child"> Child 2 </div> <div class="child"> Child 3 </div> <div class="child"> Child 4 </div> </div> <br/> <div> <div class="child"> Child 5 </div> <div class="child"> Child 6 </div> </div> <button id="del">Remove Child</button>

getElementsByClassName() returns a NodeList, which is an array-like object that presents a live view of the results. getElementsByClassName()返回一个 NodeList,它是一个类似数组的对象,用于呈现结果的实时视图。

As you remove the class from those elements, the elements are removed from the NodeList.当您从这些元素中删除类时,这些元素也会从 NodeList 中删除。

When you remove the class from child[0] , child shrinks, and child[0] becomes the next element.当您从child[0]删除类时, child缩小,而child[0]成为下一个元素。

There are a number of ways to fix this:有多种方法可以解决此问题:

  • Copy the NodeList to an array so that it doesn't change underneath you (eg, child = Array.prototype.slice.call(child) )将 NodeList 复制到一个数组中,这样它就不会在你下面发生变化(例如, child = Array.prototype.slice.call(child)

  • Loop backwards so you aren't affected by the changing indices向后循环,这样您就不会受到不断变化的索引的影响

  • Always remove the class from child[0] until the NodeList is empty.始终从child[0]删除类,直到 NodeList 为空。

Prefer using querySelector更喜欢使用querySelector

var children = document.querySelectorAll('div .child');
for (var i = 0; i < children.length; i++)
    children[i].parentNode.removeChild(children[i]);

Simple and clean ...简单干净...

 var child = document.getElementsByClassName("child"); function remove(){ var l = child.length; for (var i=0; i<l; i++) { child[0].parentNode.removeChild(child[0]); } } document.getElementById("del").onclick = remove;
 <div class="child"> Child 1 </div> <div class="child"> Child 2 </div> <div class="child"> Child 3 </div> <div class="child"> Child 4 </div> </div> <br/> <div> <div class="child"> Child 5 </div> <div class="child"> Child 6 </div> </div> <button id="del">Remove Child</button>

One has dynamic changed in DOM tree,another one doesn't.一个在 DOM 树中动态变化,另一个没有。

 function querySelectorDelete(){ var box2 = document.querySelector("#box1").querySelectorAll(".box2"); Array.prototype.forEach.call(box2,function(el){ el.remove(); }) } function getElementsDelete(){ var box2 = document.getElementById("box1").getElementsByClassName("box2"); Array.prototype.forEach.call(box2,function(el){ el.remove(); }) }
 #box1{width:300px;height:300px;background:yellow;color:#fff;} .box2{width:50px;height:50px;background:red;margin:5px;} .box3{width:100px;height:100px;background:blue;margin:5px;}
 <div id="box1"> <div class="box2">first</div> <div class="box2">second</div> <div class="box2">third</div> <div class="box3"> <div class="box2">forth</div> </div> <button type="button" onclick="querySelectorDelete()">Delete Box2</button> <button type="button" onclick="getElementsDelete()">Delete Box2</button> </div>

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

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