Javascript 如何获取<ul>中<li>的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5914110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the index of an <li> in a <ul>
提问by user695663
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.
其他地方还有一些其他<ul>
和<li>
标签。
I want to get the index of li2which is in the <ul>
with id parentusing jQuery
我想使用 jQuery获取带有 id父级的li2的索引<ul>
回答by pixelbobby
OLDsimple answer: $('ul#parent li:eq(1)').index()
旧的简单答案:$('ul#parent li:eq(1)').index()
NEW$('#li2').index()
新的$('#li2').index()
回答by Felix Kling
回答by Sang Suantak
var index = $("#li2").prevAll().length; //assuming 0 based index