开发者

How to find position number of a certain child element in a parent element using jQuery

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>

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

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

开发者_高级运维

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??))


Use index():

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜