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

标签 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>作为节点元素

最佳答案

在几乎确信我必须以经典方式遍历 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

关于javascript 如何找到包含文本的 DOM 节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52653657/

相关文章:

python - 如何在请求中获取页面标题

javascript - 在 html 页面上动态显示特定的 div

java - 使用 DOM 生成 xml 文件

javascript - 能用js切换html页面吗?

html - 编写 HTML 解析器

python - BeautifulSoup 库的 HTML 解析问题

javascript - 如何将当前播放的歌曲保存在 cookie 中?

javascript - 如何使用 jquery 使 div 可见?

javascript - 维护静态数量的可拖动元素 html/js

javascript - 具有隔离范围的 Angularjs 指令不会触发指令之外的 DOM 更改