Step-by-step learning JQuery (ii) selector: basic selector && Hierarchy Selector

Source: Internet
Author: User
Tags tag name jquery library

Selectors are the foundation of jquery, in jquery, for event handling, traversing DOM and Ajax operations all depend on selectors
Advantages of the JQuery selector:

A concise notation

A perfect event handling mechanism


First, the basic selector

The basic selector is the most commonly used selector in jQuery and the simplest selector, which finds DOM elements by element ID, class, and tag name (the ID can only be used once in a Web page, and class allows for reuse).


Change the background color of the element with ID one to # BBFFAA
Change the background color of all elements of class Mini to # BBFFAA
Change the background color of all elements named <div> to # BBFFAA
Change the background color of all elements to # BBFFAA
Change all <span> elements and the background color of the element ID is # BBFFAA

Example:

<! DOCTYPE html>
Second, level selector

If you want to get specific elements through hierarchical relationships between DOM elements, such as descendant elements, child elements, neighboring elements, sibling elements, and so on, you need to use a hierarchy selector.

Note: (the "prev ~ div") selector can only select sibling elements that follow the "# prev" element; The method siblings () in JQuery is independent of the position, as long as it is a peer node can be selected

Change the background color of all <div> in <body> to # BBFFAA
Change <body> the background color of your wife <div> # BBFFAA
Change the background color of the next <div> ID for one to # BBFFAA
Change the background color of all brothers <div> elements behind the element ID is # BBFFAA
Change the element with ID to both all <div> sibling elements with a background color of # BBFFAA

Example:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >

Step-by-step learning JQuery (ii) selector: basic selector && Hierarchy Selector

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.