如何选择索引值在一定范围内的列表项? [英] how to select list-items having index value within a certain range?

查看:213
本文介绍了如何选择索引值在一定范围内的列表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to assign a class to the list-items of an ul li list having .index() value between 10 to 20.

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

How do I select those elements using 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 获取选择中元素的子集:

Use slice to get a subset of the elements in a selection:

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

这篇关于如何选择索引值在一定范围内的列表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆