Simple explanation of the child element filter selector in jquery _jquery

Source: Internet
Author: User

The filtering rule of a child element filter is to obtain the corresponding element through the relationship between the parent element and the child element.

$ (' Li:first-child '). CSS (' background ', ' #ccc '); The first LI element of each parent element 
 
$ (' Li:last-child '). CSS (' background ', ' #ccc ');//The last Li element of each parent element 
 
$ (' Li:only-child '). CSS (' Background ', ' #ccc '); Each parent element has only one LI element 
 
$ (' Li:nth-child (Odd) '). CSS (' background ', ' #ccc ');//per parent element odd Li element 
 
$ (' Li:nth-child (even) '). CSS (' background ', ' #ccc '); Every parent element even LI element 
 
$ (' Li:nth-child (2) '). CSS (' background ', ' #ccc ');//The third Li element of each parent element 

We know that using

: A

The filter selector can get the first child element in the specified parent element, but the selector returns only one element, not a collection, and uses the

: First-child

The child element filter selector can get the first child element returned in each parent element, which is a collection that is commonly used for selection of multiple collection data.

As shown below, if you want to get the first Li in each UL in the page and change its color. You can use the

: First-child

<body>
   
 
<script type= "Text/javascript" >
  $ ("Li:first-child"). CSS ("Color", "red");
</script>

Effects displayed in the browser:

Pass

$ ("Li:first-child")

Selector code, gets the first <li> element in two <ul> parent elements, and uses the

CSS ()

Method modifies the color of the text they display on the page.

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.