简体   繁体   English

你如何循环遍历 javascript 中未知数量的孩子和孩子的孩子

[英]How do you loop through an unknown amount of children and children of children in javascript

I'm trying to loop through html that looks like this...我正在尝试遍历看起来像这样的 html...

    <div>
        <span>
            ...
        </span>
        <span>
            ...
        </span>
        <span>
            ...
        </span>
        <span>
            ...
        </span>
        <div>
            <header>
                ...
            </header>
            <div>
                ...
            </div>
            <div>
                ...
            </div>
            ...
        </div>
        <div>
            <header>
                ...
            </header>
            <div>
                ...
            </div>
            <div>
                ...
            </div>
            ...
        </div>
        <div>
            <header>
                ...
            </header>
            <div>
                ...
            </div>
            <div>
                ...
            </div>
            ...
        </div>
        ...
    </div>

I need to get data that's inside each of the divs of the div children, ignoring all of the header elements and span elements.我需要获取 div 子项的每个 div 中的数据,忽略所有 header 元素和 span 元素。 There are an unknown amount of div children elements and also an unknown number of divs within each div child.每个 div 子元素中有未知数量的 div 子元素和未知数量的 div。 Is there a way I can loop through this, I've been trying things for an hour now, and I'm not any closer to doing it.有没有办法可以遍历这个,我已经尝试了一个小时,但我离做这件事还差得远。

This is what I've tried doing, I'm using selenium.这是我尝试过的,我正在使用 selenium。

let i = 4
while(driver.findElement(By.xpath(`//*[@id="football"]/div/div/div/div[${i}]`))){
    let j = 1
    while(driver.findElement(By.xpath(`//*[@id="football"]/div/div/div/div[${i}]/div/div/div[${j}]`))){
        console.log(i, j)
        j++
    }
    i++
}

This just logs 4, 1 and then 4, 2 and then 4,3 and so on...这只是记录 4, 1 然后 4, 2 然后 4,3 等等......

This is a very simple and naive example of a recursive "algorithm" to get all the descendants of a given DOM node.这是递归“算法”的一个非常简单和幼稚的示例,用于获取给定 DOM 节点的所有后代。

 const findAllChildren = (root) => root.children.length > 0? [root, [...root.children].flatMap(findAllChildren)].flat(): root; console.log( findAllChildren(document.body) );
 <p>Hello</p> <div>Hello <div>World</div> </div> <div>Hello <div>World <div>Hello <div>World</div> </div> </div> </div>

I hope you can use this to make your own, better, implementation.我希望您可以使用它来制作自己的更好的实现。


If you only want the div elements at the bottom ( [<div>World</div>, <div>World</div>] from the above example) you can do the following:如果您只想要底部的 div 元素(上例中的[<div>World</div>, <div>World</div>] ),您可以执行以下操作:

 const findAllChildren = (root) => root.children.length > 0? [...root.children].flatMap(findAllChildren): root; console.log( [...document.querySelectorAll('body > div')].flatMap(findAllChildren) );
 <p>Hello</p> <div>Hello <div>World</div> </div> <div>Hello <div>World <div>Hello <div>World</div> </div> </div> </div>

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

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