How do I make this loop all children recursively?

前端 未结 10 575
情书的邮戳
情书的邮戳 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:34
    TreeNode node = tv.SelectedNode;
    while (node.Parent != null)
    {
        node = node.Parent;
    }                    
    CallRecursive(node);
    
    
    private void CallRecursive(TreeNode treeNode)`
    {            
        foreach (TreeNode tn in treeNode.Nodes)
        {
            //Write whatever code here this function recursively loops through all nodes                 
            CallRecursive(tn);
        }
    }
    
    0 讨论(0)
  • 2020-12-24 12:38

    If you have jquery and you want to get all descendant elements you can use:

     var all_children= $(parent_element).find('*');
    

    Just be aware that all_children is an HTML collection and not an array. They behave similarly when you're just looping, but collection doesn't have a lot of the useful Array.prototype methods you might otherwise enjoy.

    0 讨论(0)
  • 2020-12-24 12:45

    To get all descendants as an array, use this:

    function getAllDescendants(node) {
        var all = [];
        getDescendants(node);
    
        function getDescendants(node) {
            for (var i = 0; i < node.childNodes.length; i++) {
                var child = node.childNodes[i];
                getDescendants(child);
                all.push(child);
            }
        }
        return all;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 12:50

    There's no need for calling the 'allDescendants' method on all children, because the method itself already does that. So remove the last codeblock and I think that is a proper solution (á, not thé =])

                function removeTest(child){     
                    if(hasClass(child, "lbExclude")){
                        child.parentNode.removeChild(child);
                    }
                }
    
                function allDescendants (node) {
                    for (var i = 0; i < node.childNodes.length; i++) {
                      var child = node.childNodes[i];
                      allDescendants(child);
                      removeTest(child);
                    }
                }           
    
                var children = allDescendants(temp);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题