Using querySelectorAll(). Is the result returned by the method ordered?

后端 未结 1 1020
粉色の甜心
粉色の甜心 2020-12-11 00:06

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 or

相关标签:
1条回答
  • 2020-12-11 00:55

    The returned node list is ordered. A quick test proved it:

    document.querySelectorAll("body, head")[0]; //Returned [object HTMLHeadElement]
    

    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 shows body before `head.

    From http://www.w3.org/TR/selectors-api/#queryselectorall:

    The querySelectorAll() method on the NodeSelector interface must, when invoked, return a NodeList containing all of the matching Element nodes within the node’s subtrees, in document order. If there are no such nodes, the method must return an empty NodeList.

    0 讨论(0)
提交回复
热议问题