简体   繁体   English

获取具有相同类的其他元素之间的元素索引

[英]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: 这是小提琴:

http://jsfiddle.net/Pg9XQ/ http://jsfiddle.net/Pg9XQ/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取具有相同类的元素的索引 - Get the index of elements with same class 按索引选择元素(同一类的多个元素) - Select element by index (multiple elements of same class) 按下同一元素上的按钮时,从同一 class 的多个元素中删除特定元素 - Remove a specific element from among several elements of the same class when pressing the button on the same element 对具有相同类的其他元素进行动画处理,但不对此元素进行动画处理 - Animate other elements with same class, but not this element 如何获取被单击的元素的类名并使用相同的类名来操纵其他元素? - How to get the class name of the element that was clicked on and use that same class name to manipulate the other elements? 识别具有相同第一个元素的数组并根据索引对其他元素求和 - Identify arrays with same first element and sum other elements based on index jQuery获取匹配元素类型之间的索引 - jQuery get index among matched element type 仅选择单击元素的“此”对象(而不选择同一类中的其他元素) - Select 'this' object of clicked element only (not the other elements within the same class) 单击其他类别中具有相同类别的链接,而其他链接名称中单击相同的链接名称 - Click a link having same class among other classes and same link name among other link name 如何获得具有两个相同元素的数组中的索引元素? - How to get index element in array with two the same elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM