简体   繁体   English

jQuery-从具有特定类的元素列表中获取单击项的索引

[英]jQuery - Get index of clicked item from list of elements with specific class

I have a collection of elements in this format: 我有以下格式的元素集合:

<div class="row feed">

    <div class="col-sm-4 inner"><span></span></div>
    <div class="col-sm-4 inner"><span></span></div>
    <div class="col-sm-4 inner"><span></span></div>

    <div class="clearfix visible-sm"></div>

    <div class="col-sm-4 inner"><span></span></div>
    <div class="col-sm-4 inner"><span></span></div>
    <div class="col-sm-4 inner"><span></span></div>

    <div class="clearfix visible-sm"></div>

</div>

I am trying to get the index of the parent when a span is clicked. 单击span时,我试图获取父级的索引。 This works fine but if I click the 4th, 5th or 6th then the number is incorrect as it counts the clearfix div as an index. 这可以正常工作,但是如果我单击第四,第五或第六,则该数字不正确,因为它会将clearfix div计为索引。

I can't simply add 1 to the result as this format is dynamic and it grows, hence the maths could get complicated fast. 我不能简单地将1加到结果中,因为这种格式是动态的并且会不断增长,因此数学运算可能会很快变得复杂。

Instead is there a way to specify to only get the index of certain objects, I thought the following may do what I wanted but it doesn't seem to? 相反,有一种方法可以指定只获取某些对象的索引,我认为以下内容可能会实现我想要的功能,但似乎没有?

$(document).on("click", ".feed .inner span", function(){

    var activeElement = $(this).parent();

    console.log($(".feed:not(.clearfix)").index(activeElement));
});

but that just gives -1 which means no element found. 但这只给出-1 ,这意味着找不到元素。

Yes, with jQuery's .index() function you can pass an argument to tell it to get the index relative to a group of elements. 是的,使用jQuery的.index()函数,您可以传递参数来告诉它获取相对于一组元素的索引。

 $(document).on("click", ".feed .inner span", function() { console.log($(this).parent().index('div.col-sm-6.inner')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="row feed"> <div class="col-sm-6 inner"><span>1</span></div> <div class="col-sm-6 inner"><span>2</span></div> <div class="col-sm-6 inner"><span>3</span></div> <div class="clearfix visible-sm"></div> <div class="col-sm-6 inner"><span>4</span></div> <div class="col-sm-6 inner"><span>5</span></div> <div class="col-sm-6 inner"><span>6</span></div> <div class="clearfix visible-sm"></div> </div> 

Per the docs for .index() : 根据.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。

尝试将其更改为

console.log(activeElement.index(".inner"));

That's because the parent element of the clicked element doesn't exist in the $(".feed:not(.clearfix)") collection. 这是因为clicked元素的父元素在$(".feed:not(.clearfix)")集合中不存在。 You should select the children of the .feed element instead of the .feed element. 您应该选择.feed元素的.feed而不是.feed元素。 One option is: 一种选择是:

$(".feed > div:not(.clearfix)").index(activeElement);

You were close. 你近了 You need to get the index relative to the direct children of the .feed element. 您需要获取相对于.feed元素的直接子元素的索引。

The parent element of this isn't in the root of the collection .feed:not(.clearfix) , therefore you need to select the direct children of .feed instead. 的父元素this是不是在收集根.feed:not(.clearfix)因此,你需要选择的直接孩子.feed代替。 In doing so, the index will be relative to the direct children elements of .feed rather than .feed itself. 这样,索引将相对于.feed的直接子元素而不是.feed本身。

In the example below, the direct child selector, > , is used: 在下面的示例中,使用直接子选择器>

$(document).on("click", ".feed .inner span", function() {
    var index = $(".feed > :not(.clearfix)").index($(this).parent());
});

Why you only try to add a property to your html 为什么只尝试向HTML添加属性

then you only need something like this $( "[data-index='1']" ) 那么您只需要这样的东西$(“ [data-index ='1']”)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM