简体   繁体   中英

How to get the position of an element inside another element using jQuery?

If I click on the 1st div I want to get either 0 or 1 and if the second div 2 and so on...

None of my attempts are working:

http://jsfiddle.net/DZh8k/

$('div').click(function() {

    //alert( $('#something').index($(this)) );
    //alert( $(this).index( $('#something') ) );
    console.log( $('#something').find($(this)).eq() );

});


<span id="something">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</span>

Just $(this).index() will work.

http://jsfiddle.net/DZh8k/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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