使用 querySelectorAll()。方法返回的结果是有序的吗?
我正在尝试编写一个适用于多个页面的js代码。 我正在尝试使用 querySelectorAll() 来获取 DOM 中的元素。
我需要订购元素。为此,我可以使用 xPath 或选择器(我更喜欢使用选择器,但 xPath 也可以)。问题是:
querySelectorAll() 返回的 NodeList 中的元素是否按照标签在 HTML 中出现的顺序排序?
注意:我想添加标签:querySelectorAll
I'm trying to make a js code that works with multiple pages.
I'm trying to use querySelectorAll() to obtain the elements form the DOM.
I need the elements to be ordered. In order to do that I may use xPath or selectors (I'd prefer to use selectors but xPath is also ok). The problem is:
Are the elements in the NodeList returned by querySelectorAll() ordered against the order that the tags appear in the HTML?
Note: I'd like to add the tag: querySelectorAll
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回的节点列表是有序的。快速测试证明了这一点:
显然,在 HTML 文档中
标记出现在
之前。 NodeList 的第一个元素也是
元素,即使选择器在 `head 之前显示
body
也是如此。来自http://www.w3.org/TR/selectors-api/#queryselectorall:
The returned node list is ordered. A quick test proved it:
Obviously, the
<head>
tag appears before<body>
in a HTML document. The first element of the NodeList is also a<head>
element, even if the selector showsbody
before `head.From http://www.w3.org/TR/selectors-api/#queryselectorall: