有没有用于查询评论节点的DOM API? [英] Is there a DOM API for querying comment nodes?
问题描述
我有一个文档,其中有一个调试注释,如下所示:
I have a document with a debugging comment in it that looks like this:
<!--SERVER_TRACE {...}-->
有没有办法查询DOM来访问这个节点?我正在寻找一个香草JavaScript解决方案,没有任何图书馆的帮助。
Is there a way to query the DOM to access this node? I am looking for a vanilla JavaScript solution, without the aid of any libraries.
我的第一个想法是深入首先搜索DOM并比较节点类型值作为评论, Node.COMMENT_NODE
。有一个更简单的方法吗?
My first thought was to depth first search the DOM and compare nodes found against the node type value for comments, Node.COMMENT_NODE
. Is there an easier way?
推荐答案
有 TreeWalker
API: / p>
There's the TreeWalker
APIs:
var tw = document.createTreeWalker(document, NodeFilter.SHOW_COMMENT, null, null),
comment;
while (comment = tw.nextNode()) {
// ...
}
IE8不支持此功能。
This isn't supported by IE8 and lower.
TJ在评论中提供了一个链接到规格。我总是只使用TreeWalkers,但在你的情况下,一个 NodeIterator
也很好。
T.J. in the comments provided a link to the specs. I kind of always use just TreeWalkers, but in your case a NodeIterator
is fine too.
这篇关于有没有用于查询评论节点的DOM API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!