jQuery - 获取具有特定类的元素的索引 [英] jQuery - get the index of a element with a certain class

查看:1225
本文介绍了jQuery - 获取具有特定类的元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的列表:

I have a list like this one:

<li> .... </li>
<li> .... </li>
<li> .... </li>
<li class="active"> .... </li>
<li> .... </li>

我想查找带有活动的项目的索引(列表中的数字)类元素。
在这种情况下,索引将是4(如果我们从0开始,则为3)
我该怎么做?

I want to find out the index (number in the list) of the item with the "active" class element. in this case the index would be 4 (or 3 if we're starting from 0) How can I do that?

推荐答案

使用 .index()

$('li.active').index()

此处的工作示例:

http ://jsfiddle.net/EcZZL/

编辑 - 为 .index()根据尼克的建议

Edit - added link to the api for .index() per Nick's advice

这篇关于jQuery - 获取具有特定类的元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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