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
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
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
<script type= "Text/javascript" >
$ ("Li:first-child"). CSS ("Color", "red");
</script>
Effects displayed in the browser:
Pass
Selector code, gets the first <li> element in two <ul> parent elements, and uses the
Method modifies the color of the text they display on the page.