如何使用jQuery在父元素中查找某个子元素的位置编号

问题描述:

我有一个div,其中包含几个包含图像的div。看起来很像这样

I have a div containing several other divs containing an image. It looks smth like this

<div id="parentHldr">
 <div class="imgHldr"><img src="foo/bar.png" id="1"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="2"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="3"></div>
 <div class="imgHldr active"<img src="foo/bar.png" id="4"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="5"></div>
 <div class="imgHldr"><img src="foo/bar.png" id="6"></div>
</div>

我想知道具有类活动的div的位置。我得到了这个东西的子元素总数

I want to know the position of div that has class active. I get total number of children elements with this thing

$('#parentHldr').children().length

所以我认为,应该有办法以某种方式找到该div的位置编号......

So i presume, there should be a way of finding the positional number of that div somehow...

好的,我几乎找到了解决方案。现在它有点复杂了。我需要在父中获取包含 img id 5 DIV w / 类imgHldr 的索引> DIV w / class parentHldr 。这可能吗??)

ok, i've practically found the solution. Now its a bit more complicated. I need to get index of a DIV w/ class imgHldr containing img with id 5 inside the parent DIV w/ class parentHldr. Is this possible??))

使用 index()

$("#parentHldr > div").index($("#parentHldr > div.active"));