javascript - 获取具有相同类的其他元素中的元素的索引

标签 javascript jquery html

假设我有一组 div:

<div class="index-me"></div>
<div class="ignore-me"></div>
<div class="ignore-me"></div>
<div class="index-me"></div>

我如何使用 jQuery .index() 以便当我针对此列表中的最后一个元素运行它时,它返回 1,而不是 3?因此,我希望将任何元素但具有某些特定类的元素排除在索引之外。 谢谢!

最佳答案

$('div.index-me:eq(1)').index('div.index-me')

作为.index() docs show ,您可以传递选择器或元素以使索引相对于:

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

If .index() is called on a collection of elements and a DOM element or jQuery object is passed in, .index() returns an integer indicating the position of the passed element relative to the original collection.

If a selector string is passed as an argument, .index() returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector. If the element is not found, .index() will return -1.

关于javascript - 获取具有相同类的其他元素中的元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24659247/

相关文章:

javascript - 如何获取焦点上单击元素的数据属性

javascript - 谷歌翻译将英语设置为默认,以便翻译回来

javascript - 关联点击事件和多个item,没有multiple functions

html - 如何删除影子根(用户代理)

javascript - CSS3D矩阵生成

javascript - 使用搜索框通过 javascript 更改图像

javascript - OpenLayers 保存特征

jquery - 悬停时触发单击选择框

php - 下载由PHP中的多个文件组成的Zip文件

javascript - 选择框的特殊行为(需要您的意见)