[英]jQuery - get index of element, restricted to class
Given the following html: 鉴于以下html:
<div class="my-container">
<div class="x">
<a href="#">Link 1</a>
</div>
<div class="x">
<a href="#">Link 2</a>
</div>
<div class="x">
<a href="#">Link 3</a>
</div>
<div class="x y">
<a href="#">Link 4</a>
</div>
<div class="x">
<a href="#">Link 5</a>
</div>
<div class="x y">
<a href="#">Link 6</a>
</div>
<div class="x">
<a href="#">Link 7</a>
</div>
</div>
Which elements get the y
class - is a dynamic thing, which changes during runtime based on different user interactions. 哪些元素获得y
类-是动态的事物,在运行时会根据不同的用户交互而发生变化。
On mouse over an anchor (I can assume that the anchor is in a div with the y
class, because only those are visible), I need to get the index of it's container (that div with the y
class), but restricted to that y
class. 在将鼠标悬停在锚点上时(我可以假定锚点位于y
类的div中,因为只有那些可见),我需要获取其容器的索引(带有y
类的div),但仅限于此y
班。
Meaning: 含义:
0
(first element with class y
) 将鼠标悬停在“链接4”上应告诉我: 0
(第一个元素为y
) 1
(second element with class y
) 将鼠标悬停在“链接6”上应该告诉我: 1
(第二个元素,类y
) .index()
doesn't help me here .index()
在这里没有帮助我
EDIT: @Kevin B I've read the docs, but couldn't make it work. 编辑: @Kevin B我已经阅读了文档,但无法使其正常工作。 The closest thing I could find there was to pass a collection to .index()
, which I've tried. 我能找到的最接近的东西是将集合传递给我尝试过的.index()
。 But didn't work (also, their example for the collection is with vanilla js document.getElementById - that didn't work for me, need to work with classes; tried to adapt: myCollection = $(this).closest('.my-container').children('.y') and passed that to .index()
, and it didn't work). 但是没有用(同样,他们的集合示例是使用vanilla js document.getElementById-对我不起作用,需要使用类;试图适应:myCollection = $(this).closest('。 my-container')。children('。y')并将其传递给.index()
,但无效。 I wouldn't post without google-ing first and also going through the docs, don't know why the down vote (not pointing any fingers, I'm not assuming I know who's is it). 我不会在没有Google-ing的情况下发布文档,也不会浏览文档,不知道为什么会投票(不指责,我不假设我知道是谁)。 Just because I said ".index()" doesn't help me"? Well, I've tried whatever I understood I could do with it, and couldn't make it happen. That's why I posted. 仅仅因为我说“ .index()”对我没有帮助”吗?好吧,我尝试了我认为可以解决的所有事情,但并没有实现它,这就是我发布的原因。
As said in the comments, index is exactly what you need: 如评论中所述,索引正是您需要的:
$(document).ready(function() {
//mousein
$("a").hover(function(){
var parent = $('.my-container').eq(2); // the 3rd "my-container"
console.log(parent.find('.y a').index(this)); //-1 if elm doesnt exist
},
//mouseout
function(){
})
});
As said in the comments, index is exactly what you need: 如评论中所述,索引正是您需要的:
$(document).ready(function() {
//mousein
$("a").hover(function(){
console.log($(this).index('.y a')); //-1 if elm doesnt exist
},
//mouseout
function(){
})
});
var count = 1; $(".my-container div").on('mouseover',function(){ if ($(this).attr("class").indexOf('y') > -1){ alert(count + "th mouseover on y class"); count++; } })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="my-container"> <div class="x"> <a href="#">Link 1</a> </div> <div class="x"> <a href="#">Link 2</a> </div> <div class="x"> <a href="#">Link 3</a> </div> <div class="xy"> <a href="#">Link 4</a> </div> <div class="x"> <a href="#">Link 5</a> </div> <div class="xy"> <a href="#">Link 6</a> </div> <div class="x"> <a href="#">Link 7</a> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.