[英]Is there any possibility to get all HTML content elements that contains “plain” text values from HTML document using javascript?
Is there any possibility to get all HTML content elements that contains "plain" text values from HTML document using javascript? 是否有可能使用javascript从HTML文档中获取包含“纯”文本值的所有HTML内容元素?
For example: 例如:
<html>
<body>
<div>
Text1
<p>
Text2
</p>
</div>
</body>
</html>
I want to get Text1 and Text2. 我想获取Text1和Text2。
Sure, you can simply iterate over the DOM nodes: 当然,您可以简单地遍历DOM节点:
function getTextNodes(node) {
var result = [];
for(var child = node.firstChild; child; child = child.nextSibling) {
if(child.nodeType === 3) { // text node
result.push(child);
}
else if(child.nodeType === 1) { // element node
result = result.concat(getTextNodes(child));
}
}
return result;
}
var textNodes = getTextNodes(document.body);
This is a recursive approach, you can also select all element nodes first and then get their child text nodes. 这是一种递归方法,您也可以先选择所有元素节点,然后再获取其子文本节点。
You probably also want to filter out text nodes only containing whitespaces. 您可能还希望过滤掉仅包含空格的文本节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.