How to get a collection of nodes element node or element by CSS selectors

Use document.querySelector () and document.querySelectorAll () , the CSS selector as an argument can be.

// tag selector 
document.querySelector ( "div" ); 

// ID selector 
document.querySelector ( "# App" ); 

// set selector 
document.querySelector ( "body div");

 

note: 

1. querySelecotor () Returns an element node, querySelectorAll () Returns a NodeList instance object;

2. Both methods support complex CSS selectors, but does not support pseudo-classes and pseudo-element selector;

3. The two methods not only in the Document, but also defined on the element node, such as this may be used: document.body.querySelector ()

Guess you like

Origin www.cnblogs.com/aisowe/p/11542578.html