javascript 如何找到包含文本的 DOM 节点?
javascript how to find that DOM node that contains a text?
给定一个获取的 html 页面,我想找到包含一部分文本的特定节点。我想最困难的方法是一个一个地迭代所有节点,尽可能深入,并针对每种情况进行搜索,例如.includes()
但是怎样才是明智之道呢?一定有什么东西,但我无法 google 正确地
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
给定一个获取的 html 页面,我想找到包含一部分文本的特定节点。我想最困难的方法是一个一个地迭代所有节点,尽可能深入,并针对每种情况进行搜索,例如.includes()
但是怎样才是明智之道呢?一定有什么东西,但我无法 google 正确地
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