javascript如何找到包含文本的DOM节点?

时间:2018-10-04 19:14:20

标签: javascript dom html-parsing domparser

给定一个html页面,我想找到包含一部分文本的特定节点。我想这很难的方法是一步一步地遍历所有节点,直到深入为止,并针对每种情况进行例如.includes()

但是明智的方法是什么?一定有东西,但是我无法正确搜索它

    response = axios.get(url);
    let parsedHtml = parser.parseFromString(response.data, 'text/html');
    for (let i = 0; i < parsedHtml.children.length; i++)
       if (parsedHtml.children[i].textContent.includes('hello'))
          console.log(parsedHtml.children[i])

*不起作用

*示例代码

<html>
 <body>
  <div>dfsdf</div>
  <div>
   <div>dfsdf</div>
   <div>dfsdf</div>
  </div>
  <div>
   <div>
    <div>hello</div>
   </div>
  </div>
  <div>dfsdf</div>
 </body>
 </html>

我想检索<div>hello</div>作为节点元素

1 个答案:

答案 0 :(得分:1)

在几乎确信我必须以经典方式遍历DOM之后,我在这里Javascript: How to loop through ALL DOM elements on a page?发现了这一点,这确实很棒:

    let nodeIterator = document.createNodeIterator(
        parsedHtml,
        NodeFilter.SHOW_ELEMENT,
        (node) => {
            return (node.textContent.includes('mytext1')
                || node.textContent.includes('mytext2'))
                && node.nodeName.toLowerCase() !== 'script' // not interested in the script
                && node.children.length === 0 // this is the last node
                ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
        }
    );
    let pars = [];
    let currentNode;

    while (currentNode = nodeIterator.nextNode())
        pars.push(currentNode);
    console.log(pars[0].textContent); // for example