In fact, there are many descriptions about queryselector and queryselectorall.
This is mainly a record.
At the beginning, many people will use the jquery selector to compare with the two APIs (I am also). The similarities and differences are actually okay, however, some people misunderstand the implementation of these two APIs in the browser, especially when calling this API on Dom element.
The following is my jsfiddle example. I will describe it here:
The misunderstanding lies in $ ('inner '). queryselectorall ('# Main h4.inside') Implementation understanding, many people initially think that it is directly from the DIV [ID = 'inner '] Child Search (I am also ), this # Main is a bit confusing. In fact, it searches for the entire document based on the selector string and returns the child node that belongs to the DIV [ID = 'inner. Many people may wonder why it is not implemented simply by finding Child Nodes Based on the parent node? Like ELEM. getelementsbytagname, my idea is flexible selector string.
Queryselector returns only the first Matching Element. If no matching item exists, null is returned.
Queryselectorall returns a set of matched elements. If no matching item exists, an empty nodelist (node array) is returned ).
The returned results are static, and subsequent changes to the document structure will not affect the previous results.
Currently, IE8 +, ff, and chrome support this API (selector string in IE8 only supports css2.1 ).