简体   繁体   中英

selecting 2nd and 3rd list items in a list with jquery

I have a list.

<ul id="navigation">
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>

And using jquery id like to apply a clas to the 2nd and 3rd list items.

Is there simple code for me to do this?

Thanks

最简单的方法是使用逗号分隔符将第二和第三列表项分组:

$("#navigation li:nth-child(2), #navigation li:nth-child(3)").addClass("name");
$("#navigation li:eq(1), #navigation li:eq(2)").addClass("someClass");

看看:eq选择器。

While Cletus is right, and the simplest thing you can do is use the standard jQuery comma-separated list, if it turns out you need to choose a whole lot of them, you should start looking at the .nextUntil() and .prevUntil() methods. You'd use them like so:

$("#navigation li:nth-child(2)").nextUntil(":nth-child(4)").addClass("name");

试试吧

$("#navigation li:gt(0):lt(2)").addClass("t");

您正在寻找第n个子选择器。

$("ul li:nth-child(2)").addClass('MyClass');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM