Is there any possibility to get all HTML content elements that contains "plain" text values from HTML document using javascript?
For example:
<html>
<body>
<div>
Text1
<p>
Text2
</p>
</div>
</body>
</html>
I want to get Text1 and Text2.
Sure, you can simply iterate over the DOM nodes:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.