jquery查找元素的位置
<ul>
<li>one</li>
<li>element</li>
<li>text</li>
<li>val</li>
</ul>
我怎样才能获得点击的li
在ul
中的位置?
<ul>
<li>one</li>
<li>element</li>
<li>text</li>
<li>val</li>
</ul>
how can i get the position in the ul
of the clicked li
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这会为你做到这一点:
请注意,index()函数返回jquery集合中项目的索引。如果页面上有多个列表,请确保选择器仅选择所需的列表项。
I think this will do it for you:
Note that the index() function returns the index of the item in the jquery collection. If you have multiple lists on the page, make sure your selector only selects the list items that you want.
参考
我只测试了一个
。如果您在多个
上执行此操作,则需要
.each
。Reference
I only tested with one
<ul>
. You'll need to.each
if you're doing this on multiple<ul>
s.你可以用index() http://docs.jquery.com/Core/index
you can do it with index() http://docs.jquery.com/Core/index