查找HTML页面中的所有文本节点 [英] Find all text nodes in HTML page
问题描述
对于这个问题我需要找到特定节点下的所有文本节点。我可以这样做:
function textNodesUnder(root){
var textNodes = [];
addTextNodes(root);
[] .forEach.call(root.querySelectorAll('*'),addTextNodes);
返回textNodes;
$ b $ function addTextNodes(el){
textNodes = textNodes.concat(
[] .filter.call(el.childNodes,function(k){
return k .nodeType == Node.TEXT_NODE;
})
);
$ / code>
然而,这看起来很不雅观,使用XPath,可以简单地查询 .// text()
并完成它。
在IE9 +,Safari5 +,Chrome19 +,Firefox12 +,Opera11 +上使用HTML文档中的特定元素下的所有文本节点的最简单方法是什么?
最简单被定义为高效而简短,没有高尔夫球。
基于@ kennebec的回答,稍微严格的实现相同的逻辑:
function textNodesUnder(node){
var all = []; (node.firstChild; node; node = node.nextSibling){
if(node.nodeType == 3)all.push(node);
else all = all.concat(textNodesUnder(node));
}
返回所有;
}
然而,更快,更紧密, a href =https://developer.mozilla.org/en/DOM/document.createTreeWalker> createTreeWalker
,以便浏览器过滤除了为你的文本节点:
function textNodesUnder(el){
var n,a = [] ,walk = document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n = walk.nextNode())a.push(n);
返回a;
}
Possible Duplicate:
getElementsByTagName() equivalent for textNodes
For this question I needed to find all text nodes under a particular node. I can do this like so:
function textNodesUnder(root){
var textNodes = [];
addTextNodes(root);
[].forEach.call(root.querySelectorAll('*'),addTextNodes);
return textNodes;
function addTextNodes(el){
textNodes = textNodes.concat(
[].filter.call(el.childNodes,function(k){
return k.nodeType==Node.TEXT_NODE;
})
);
}
}
However, this seems inelegant in light of the fact that with XPath one could simply query for .//text()
and be done with it.
What's the simplest way to get all text nodes under a particular element in an HTML document, that works on IE9+, Safari5+, Chrome19+, Firefox12+, Opera11+?
"Simplest" is defined loosely as "efficient and short, without golfing".
Based on @kennebec's answer, a slightly tighter implementation of the same logic:
function textNodesUnder(node){
var all = [];
for (node=node.firstChild;node;node=node.nextSibling){
if (node.nodeType==3) all.push(node);
else all = all.concat(textNodesUnder(node));
}
return all;
}
However, far faster, tighter, and more elegant is using createTreeWalker
so that the browser filters out everything but the text nodes for you:
function textNodesUnder(el){
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()) a.push(n);
return a;
}
这篇关于查找HTML页面中的所有文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!