gpt4 book ai didi

jquery - 如何选择索引值在一定范围内的列表项?

转载 作者:行者123 更新时间:2023-12-01 06:39:54 26 4
gpt4 key购买 nike

我想将一个类分配给 ul li 列表中 .index() 值在 10 到 20 之间的列表项。

如何使用 jQuery 选择这些元素?

<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
...
...
...
...
li>Thirty</li>
</ul>

所以我想要类似的东西

$('#id').click(function(){
//Assign class 'classname' to those li elements
//between 10th and 20th (index numbers) in the list
if([li element's index is between 10 and 20]).addClass('classname');
});

最佳答案

使用slice获取选择中元素的子集:

$('li').slice(10, 20).addClass('classname');

关于jquery - 如何选择索引值在一定范围内的列表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6250135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com