select only third li
how do i select only 3rd( or some other no. of my chioce) li element with jquery? for ex: how do i change the background o开发者_开发知识库f only third li with jquery. any help please
how do i select only 3rd(
Use the eq
method like this:
$('li').eq(2).css('background', 'yellow');
Or you can use this variation of :eq
filter selector:
$('li:eq(2)').css('background', 'yellow');
The indexing starts from 0
, you need to specify 2
to actually select the third li
If however you want to select every third element, you need to use nth-child
like this:
$('li:nth-child(3n)')
The index for nth-child
starts from 1
though.
If you need the third li
on all lists, use nth-child
:
$('li:nth-child(3)')
精彩评论