QuerySelector and querySelectorAll are new query interfaces provided by W3C. Their main features are as follows:
1. querySelector returns only the first matching element. If no matching item exists, null is returned. 2. querySelectorAll returns a set of matched elements. If no matching item exists, an empty nodelist (node array) is returned ). 3. The returned results are static. Subsequent changes to the document structure will not affect the previous results. Both methods can accept three types of parameters: id (#), class (.), and label, much like the jquery selector.
Var obj = document. querySelector ("# id"); var obj = document. querySelector (". classname"); var obj = document. querySelector ("div ");
Var elements = document. querySelectorAll ("# score> tbody> tr> td: nth-of-type (2)"); var elements = document. querySelectorAll ("# id1, # id2 ,. class1, class2, div a, # list li img ");
Document. querySelector and querySelectorAll methods