简体   繁体   English

获取同级兄弟中元素的索引,而忽略具有特定子级的同级兄弟

[英]Get index of element among siblings ignoring sibling with specific child

I have a specific use case I can't seem to find an answer to. 我有一个特定的用例,但似乎找不到答案。 Given the DOM elements below: 鉴于以下DOM元素:

<div class="wrapper">
   <div class="item"></div>
   <div class="item">
       <div class="foo"></div>
   </div>
   <div class="item">
       <div class="bar"></div>
   </div>
   <div class="item"></div>
   <div class="item selected"></div>
   <div class="item"></div>
</div>

I need to find the index of the .selected element in regard to it's siblings. 我需要找到.selected同级的.selected元素的索引。 But I need to ignore any siblings that have the .foo child element (it will only ever be the direct child). 但是我需要忽略任何具有.foo子元素的兄弟姐妹(它将永远是直接子元素)。

So typically to find the index of .item .selected you could use $(".item.selected").index() which gives 4 , but since one item before it has a .foo child the correct answer is 3 . 因此,通常情况下,找到.item .selected的索引可以使用$(".item.selected").index()给出4的结果 ,但是由于前一个项目具有.foo子级,因此正确答案是3

I thought, the best way to go about it was to grab all the siblings before the selected element (since siblings after it wouldn't shift it's index) and then count how many have a .foo child, and subtract that number from the selected index, so 4-1=3 . 我认为,最好的方法是在选定元素之前抓住所有同级兄弟(因为之后的同级兄弟不会移动索引),然后计算有一个.foo子代,然后从选定的子代中减去该数字索引,所以4-1 = 3 I tried to do that like this: 我试图这样做:

var selectedIndex = $(".item.selected").index();
var fooCount = $(".item.selected").prevAll('.item > .foo').length;
var finalIndex = selectedIndex - fooCount;

The problem is, fooCount is coming up 0 instead of 3 . 问题是,fooCount变为0而不是3

You can simply use .filter() and remove the preceding elements that have a given child. 您可以简单地使用.filter()并删除具有给定子级的先前元素。

 const selected = $('.selected'); const foos = selected.prevAll().filter(function() { return !($(this).find('.foo').length); }); console.log(selected.index(), foos.length); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="item"></div> <div class="item"> <div class="foo"></div> </div> <div class="item"> <div class="bar"></div> </div> <div class="item"></div> <div class="item selected"></div> <div class="item"></div> </div> 

Use this selector ".item:not(:has(.foo))" and then loop to find the specific index. 使用此选择器".item:not(:has(.foo))" ,然后循环查找特定索引。

 var index = $(".item:not(:has(.foo))") .toArray() .findIndex(function(item) { return $(item).hasClass('selected'); }); console.log(index); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="item"></div> <div class="item"> <div class="foo"></div> </div> <div class="item"> <div class="bar"></div> </div> <div class="item"></div> <div class="item selected"></div> <div class="item"></div> </div> 

You were close. 你近了

Change: 更改:

var fooCount = $(".item.selected").prevAll('.item > .foo').length;

… to: … 至:

var fooCount = $(".item.selected").prevAll('.item:has(.foo)').length;

Otherwise, you're looking for a sibling with class .foo , when you actually want a sibling that has a child with class .foo . 否则,你正在寻找带班兄弟姐妹 .foo ,当你真正想要的是具有类儿童的兄弟姐妹.foo

Snippet: 片段:

 var selectedIndex = $(".item.selected").index(); var fooCount = $(".item.selected").prevAll('.item:has(.foo)').length; var finalIndex = selectedIndex - fooCount; console.log(finalIndex); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="item"></div> <div class="item"> <div class="foo"></div> </div> <div class="item"> <div class="bar"></div> </div> <div class="item"></div> <div class="item selected"></div> <div class="item"></div> </div> 

You can combine: 您可以结合使用:

:not() : selects all elements that do not match the given selector. :not() :选择与给定选择器不匹配的所有元素。

:has() : reduce the set of matched elements to those that have a descendant that matches the selector or DOM element. :has() :将匹配元素的集合减少为具有与选择器或DOM元素匹配的后代的元素。

:index(element) : where element is the DOM element or first element within the jQuery object to look for. :index(element) :其中element是DOM元素或要查找的jQuery对象中的第一个元素。

Hence, you can change your code: 因此,您可以更改代码:

var selectedIndex = $(".item.selected").index();

to: 至:

var selectedIndex = $('.item:not(:has(.foo))').index($('.item.selected'));

 var selectedIndex = $('.item:not(:has(.foo))').index($('.item.selected')); console.log(selectedIndex ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="item"></div> <div class="item"> <div class="foo"></div> </div> <div class="item"> <div class="bar"></div> </div> <div class="item"></div> <div class="item selected"></div> <div class="item"></div> </div> 

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

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