如何获取< li>的索引元素 [英] How to get index of <li> element
本文介绍了如何获取< li>的索引元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在调查帖子 找到元素在列表中的位置, cletus先生提到要获取索引,我们必须使用
I was looking into post Find the position of an element within a list, and Mr cletus mentioned that to get index we have to use
var index = $(this).parent().children().index(this);
HTML:
<ul>
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
</ul>
我的问题是为什么$(this).index(this)
不起作用(它始终呈现0),而$(this).html()
却呈现正确的html输出.有人可以解释吗?
My question is why $(this).index(this)
does not work(it always renders 0), whereas $(this).html()
renders proper html output. Can somebody explain?
推荐答案
由于$(this)
对<li>
元素的引用,并且通过使用$(this).index(this)
,您实际上是在询问当前元素在其内部的索引-显然是0.
Because $(this)
references to the <li>
element, and by using $(this).index(this)
, you are essentially asking the index of the current element inside itself - which obviously is 0.
这篇关于如何获取< li>的索引元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文