Javascript,dom Queryselect () and Queryselectall ()

Source: Internet
Author: User

Queryselect ()

This is relatively straightforward and straightforward, and can be understood in a few examples

Gets the first element of class= "example" in the Document: Document.queryselector (". Example");
< P > element: Document.queryselector ("P");
Assume that you have selected two selectors:<H2>And<H3>element. The following code will be the first one in the document<H2>element to add a background color, because we can only filter out the first label that exists in the filter from top to bottom :<H2>A H2 Element</H2><H3>A H3 Element</H3>Document.queryselector ("H2, H3"). Style.backgroundcolor = "Red";
< P > element: Document.queryselector ("P.example");

Queryselectall ()

As the name implies, all eligible tags are filtered out. Generate a NodeList (node list) , we can be lazy to access by subscript .

< P > element var x = Document.queryselectorall ("P");   < P > the background color of the element x[0].style.backgroundcolor = "Red";
< P > element var x = Document.queryselectorall ("P.example");   < P > the background color of the element x[0].style.backgroundcolor = "Red";
< a > element: Document.queryselector ("a[target]");
< P > the number of elements (using the Length property of the NodeList object): var len = Document.queryselectorall (". Example"). Length;
< x . length; i++) {    = "Red";}
< x . length; i++) {    = "Red";}

After reading the above, we find that the most important thing in the selector is the part in parentheses , that is, how we write the selector determines what we are filtering out.

So here's a CSS selector site that you can use for reference.

Javascript,dom Queryselect () and Queryselectall ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.