Possible Duplicates:
How to get index of <li> element jQuery - get the index of a element with a certain class
I have:
<ul id="parent">
<li id="li1">li1</li>
<li id="li2">li2</li>
<li id="li3">li3</li>
</ul>
There are some other <ul>
and <li>
tags elsewhere.
I want to get the index of li2 which is in the <ul>
with id parent using jQuery
OLD simple answer: $('ul#parent li:eq(1)').index()
NEW $('#li2').index()
Use .index()
:
$('#li2').index();
IDs have to be unique so in case they are not in your HTML, you better fix this (e.g. by using classes).
var index = $("#li2").prevAll().length; //assuming 0 based index
精彩评论