如何使用jquery或vanilla js在li中查找元素的位置 [英] How to find the position for an element in a li with jquery or vanilla js
本文介绍了如何使用jquery或vanilla js在li中查找元素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们想象一下我有以下HTML代码.
Lets imagine I have the following HTML code.
我需要找到已应用类焦点的LI的元素在LI元素中的位置.
I need to find the position within the LI elements for the LI which has the class focus applied.
在此示例中,结果应为2(如果索引为0).知道怎么做吗?
In this example the result should be 2 (if at 0 index). Any idea how to do it?
<ul class="mylist">
<li>Milk</li>
<li>Tea</li>
<li class="focus">Coffee</li>
</ul>
推荐答案
使用.index()
var index = $('.focus').index();
特定列表
$('.mylist .focus').index()
这篇关于如何使用jquery或vanilla js在li中查找元素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文