getElementsByTagName() 相当于 textNodes
有没有办法获取文档中所有 textNode
对象的集合?
getElementsByTagName()
对于元素来说效果很好,但 textNode
不是元素。
更新:我意识到这可以通过遍历 DOM 来完成 - 正如下面许多人建议的那样。我知道如何编写一个 DOM-walker 函数来查看文档中的每个节点。我希望有一些浏览器原生的方法来做到这一点。毕竟,我可以通过一个内置调用获取所有 ,但不是所有
textNode
,这有点奇怪。
Is there any way to get the collection of all textNode
objects within a document?
getElementsByTagName()
works great for Elements, but textNode
s are not Elements.
Update: I realize this can be accomplished by walking the DOM - as many below suggest. I know how to write a DOM-walker function that looks at every node in the document. I was hoping there was some browser-native way to do it. After all it's a little strange that I can get all the <input>
s with a single built-in call, but not all textNode
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
下面是最快的 TreeWalker 方法的现代
Iterator
版本:用法:
更安全的版本
如果在循环时移动节点,直接使用迭代器可能会被卡住。这样更安全,它返回一个数组:
Here's a modern
Iterator
version of the fastest TreeWalker method:Usage:
Safer version
Using the iterator directly might get stuck if you move the nodes around while looping. This is safer, it returns an array:
我知道您特别要求一个集合,但如果您只是非正式地表示这一点并且不关心它们是否全部连接在一起形成一个大字符串,您可以使用:
...第一项是 DOM3 标准方法。但请注意,
innerText
似乎在支持它的实现中排除脚本或样式标记内容(至少 IE 和 Chrome),而textContent
则包含它们(在 Firefox 和 Chrome 中)。I know you specifically asked for a collection, but if you just meant that informally and didn't care if they were all joined together into one big string, you can use:
...with the first item being the DOM3 standard approach. Note however that
innerText
appears to exclude script or style tag contents in implementations that support it (at least IE and Chrome) whiletextContent
includes them (in Firefox and Chrome).这是一个更惯用且(希望)更容易理解的替代方案。
Here's an alternative that's a bit more idiomatic and (hopefully) easier to understand.
在
createTreeWalker
被弃用后,您可以使用after
createTreeWalker
is deprecated you can use/*
您可以返回某个父元素的所有后代文本节点的数组,
或者你可以传递一些函数并做一些事情(查找或替换或其他)
到适当的文本。
此示例返回正文中非空白文本节点的文本:
*/
方便搜索和替换、突出显示等
/*
You can return an array of all the descendant text nodes of some parent element,
or you can pass it some function and do something (find or replace or whatever)
to the text in place.
This example returns the text of the non-whitespace textnodes in the body:
*/
Handy for search and replace, highlighting and so on
更新:
我概述了这 6 种方法中每种方法超过 1000 次运行的一些基本性能测试。
getElementsByTagName
是最快的,但它做得半途而废,因为它不选择所有元素,而只选择一种特定类型的标签(我认为p
)并盲目地假设它的第一个子元素是一个文本元素。它可能有一点缺陷,但它是为了演示目的并将其性能与 TreeWalker 进行比较。 在 jsfiddle 上自行运行测试以查看结果。让我们暂时假设有一种方法允许您以本机方式获取所有
Text
节点。您仍然需要遍历每个结果文本节点并调用node.nodeValue
来获取实际文本,就像处理任何 DOM 节点一样。因此,性能问题不在于迭代文本节点,而在于迭代所有非文本节点并检查它们的类型。我认为(根据结果)TreeWalker
的执行速度与getElementsByTagName
一样快,甚至更快(即使 getElementsByTagName 玩残疾人)。每种方法的来源:
TreeWalker
递归树遍历
迭代树遍历
querySelectorAll
getElementsByTagName ( handicap)
XPath
此外,您可能会发现此讨论很有帮助 - http://bytes.com/topic/javascript/answers/153239-how-do-i-get-elements-text-node
Update:
I have outlined some basic performance tests for each of these 6 methods over 1000 runs.
getElementsByTagName
is the fastest but it does a half-assed job, as it does not select all elements, but only one particular type of tag ( i thinkp
) and blindly assumes that its firstChild is a text element. It might be little flawed but its there for demonstration purpose and comparing its performance toTreeWalker
. Run the tests yourselves on jsfiddle to see the results.Let's assume for a moment that there is a method that allows you to get all
Text
nodes natively. You would still have to traverse each resulting text node and callnode.nodeValue
to get the actual text as you would do with any DOM Node. So the issue of performance is not with iterating through text nodes, but iterating through all nodes that are not text and checking their type. I would argue (based on the results) thatTreeWalker
performs just as fast asgetElementsByTagName
, if not faster (even with getElementsByTagName playing handicapped).Source for each method:
TreeWalker
Recursive Tree Traversal
Iterative Tree Traversal
querySelectorAll
getElementsByTagName (handicap)
XPath
Also, you might find this discussion helpful - http://bytes.com/topic/javascript/answers/153239-how-do-i-get-elements-text-node