How to find position number of a certain child ele

2019-06-15 13:58发布

问题:

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

回答1:

Use index():

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