How do I make this loop all children recursively?

前端 未结 10 607
情书的邮戳
情书的邮戳 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:50

    You can use BFS to find all the elements.

    function(element) {
        // [].slice.call() - HTMLCollection to Array
        var children = [].slice.call(element.children), found = 0;
        while (children.length > found) {
            children = children.concat([].slice.call(children[found].children));
            found++;
        }
        return children;
    };
    

    This function returns all the children's children of the element.

提交回复
热议问题