假设我有一组 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/