[英]Get index of an element among other elements with the same class
Assume I have a set of divs: 假设我有一组div:
<div class="index-me"></div>
<div class="ignore-me"></div>
<div class="ignore-me"></div>
<div class="index-me"></div>
How do I use jQuery .index()
so that when I run it against the last element in this list, it returns 1, not 3? 如何使用jQuery
.index()
以便在针对此列表中的最后一个元素运行它时,它返回1,而不是3? So, I want to any elements but with some certain class to be excluded from indexing. 因此,我希望将具有某些特定类的任何元素从索引中排除。 Thanks!
谢谢!
$('div.index-me:eq(1)').index('div.index-me')
As the .index()
docs show , you can pass a selector or element to make the index relative to: 如
.index()
文档所示 ,您可以传递选择器或元素以使索引相对于:
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.
如果没有参数传递给.index()方法,则返回值是一个整数,指示jQuery对象中第一个元素相对于其同级元素的位置。
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.
如果在元素集合上调用了.index(),并且传入了DOM元素或jQuery对象,则.index()返回一个整数,该整数指示所传递的元素相对于原始集合的位置。
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.
如果将选择器字符串作为参数传递,则.index()返回一个整数,该整数指示jQuery对象中第一个元素相对于选择器匹配的元素的位置。 If the element is not found, .index() will return -1.
如果找不到该元素,则.index()将返回-1。
var index = $('.index-me:last-child').index('.index-me');
Here is fiddle: 这是小提琴:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.