Matches the Nth child or odd-even element under its parent element
':eq(index)' only matches one element, And this one will match child elements for every parent element. :nth-child starts from 1, and :eq() starts from 0!
Can be used: nth-child(even) :nth-child(odd) :nth-child(3n) :nth-child(2) :nth-child(3n+1) :nth-child(3n+2)
Find the second li in each ul: $("ul li: nth-child(2)")
:first-child
matches the first child element
':first' only Matches an element, and this selector will match a child element for each parent element
Find the first li in each ul: $("ul li:first-child ")
:last-child
matches the last child element
':last'matches only one element, and this selection The symbol will match a child element for each parent element
Find the last li in each ul: $("ul li:last- child")
##:only-child
If an element is the only child element of the parent element, it will be matchedIf the parent element contains other elements, it will not be matched.
Find the li that is the only child element in ul:
$("ul li:only-child")
Note:
1. :nth-child(index) starts from 1, and eq(index) starts from 0, that is to say $(" ul li:nth-child(0)").css("color","red") cannot obtain matching elements and can only start from 1, that is $("ul li:nth-child(1) ").css("color","red"), and eq(0) can be obtained, the same is to obtain the first child element
:nth-child(even) is to obtain the even number of child elements, That is, the second, fourth, sixth..., and :even starts from index 0, matching the second index, the fourth index..., that is, the first, the third, The fifth..., it seems that they all get an odd number of items, the same is true for :nth-child (odd) and :odd
2. :first-child matches the child elements of each parent class , and :first matches a child element, and the same goes for :last-child and last
3. only-child: matches an element that is the only child element in the parent element, that is, the current child element is The only element in the class matches!
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning