How do I make this loop all children recursively?

前端 未结 10 621
情书的邮戳
情书的邮戳 2020-12-24 12:06

I have the following:

for (var i = 0; i < children.length; i++){
   if(hasClass(children[i], \"lbExclude\")){
       children[i].parentNode.removeChild(ch         


        
10条回答
  •  心在旅途
    2020-12-24 12:48

    function allDescendants (node) {
        for (var i = 0; i < node.childNodes.length; i++) {
          var child = node.childNodes[i];
          allDescendants(child);
          doSomethingToNode(child);
        }
    }
    

    You loop over all the children, and for each element, you call the same function and have it loop over the children of that element.

提交回复
热议问题