removing childNodes using node.childNodes.forEach

前端 未结 2 436
孤街浪徒
孤街浪徒 2020-12-22 00:49

Traditionally, a suggested way of removing a node\'s children in Javascript is to do something like this:

while(node.firstChild) {
    node.removeChild(node.         


        
2条回答
  •  臣服心动
    2020-12-22 01:09

    node.childNodes is a live collection, so when you remove a child from node in the forEach you mess with the iterator.

    https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes

提交回复
热议问题