jQuery child element filter



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!

   无标题页   
  • 第一个元素
  • 第二个元素
  • 第三个元素
  • 第四个元素
  • 第五个元素
  • 第六个元素
  • 第一个元素
  • 第二个元素
  • 第三个元素
  • 第四个元素
  • 第五个元素
  • 第六个元素


Continuing Learning
||
无标题页
1 2 3 4 5
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
##:only-child Find the li that is the only child element in ul:
Name Description Example
:nth -child(index/even/odd/equation)

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")
If an element is the only child element of the parent element, it will be matched

If the parent element contains other elements, it will not be matched.


$("ul li:only-child")