jQuery:在事件处理程序中获取元素索引的最佳方法 [英] jQuery: best way to get the index of an element in an event handler
本文介绍了jQuery:在事件处理程序中获取元素索引的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在事件处理程序中检索元素索引的最佳方法是什么:
What is the best way to retrieve the index of an element in an event handler:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
$("ul li").click(function() {
// what is the index of the list item that was clicked?
});
换句话说,如果我单击"c"项,是否存在从事件处理程序中获取索引2的最佳实践?
In other words, if I click the 'c' item, is there a best practice for getting the index of 2 from within the event handler?
我知道可以通过查看其父元素来确定元素的位置,但是我不知道是否有更好或更简洁的方法.
I know I can determine the element's position by looking at its parent, but I don't know if there is a better or more concise way.
推荐答案
调用 $(this).index()
.
这篇关于jQuery:在事件处理程序中获取元素索引的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文