简体   繁体   English

jquery 找到所有具有 class 的元素并返回被点击元素的计数 position

[英]jquery find all elements with class and return the count position of the clicked Element

I am not looking for $(this).index() as an FYI.我不是在寻找$(this).index()作为仅供参考。 So, what I have are two <ul> tags with <li> tags within them.所以,我有两个<ul>标签,里面有<li>标签。 I want to know which <ul> was clicked and which <li> was clicked.我想知道单击了哪个<ul>以及单击了哪个<li> For example something like ul 0 and li 3. So if there are 10 <ul> tags on the page I only want to look at the ones with the list class for clicks.例如像 ul 0 和 li 3 这样的东西。因此,如果页面上有 10 个<ul>标签,我只想查看带有list class 的标签以进行点击。

jQuery Example that will not work for what I want: jQuery 示例不适用于我想要的:

 $('ul').click(function(event) { alert($(this).index()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="list"> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> </ul> <hr /> <ul class="list"> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> </ul>

The wanted outcome of the clicked element: UL # and LI #.点击元素的期望结果:UL # 和 LI #。 Like UL0 LI3, UL1 LI3, etc...像UL0 LI3, UL1 LI3, 等等...

JSFiddle: http://jsfiddle.net/cg3a8d6n/ JSFiddle: http://jsfiddle.net/cg3a8d6n/

You could use click event on li element and then get its index and also the index of its parent which is ul .您可以在li元素上使用 click 事件,然后获取其索引以及其父级的索引ul You can also specify selector inside the index method to consider only specific elements that match the selector.您还可以在index方法中指定选择器,以仅考虑与选择器匹配的特定元素。

 $('ul.list li').click(function(event) { const li = $(this).index(); const ul = $(this).parent().index('ul.list'); console.log(`ul: ${ul} li: ${li}`) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="list"> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> </ul> <hr /> <ul></ul> <ul></ul> <ul class="list"> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> </ul>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到先前单击的类/元素-jQuery - how to find the previous clicked class / element - jquery 使用jQuery在元素数组中查找元素的位置 - Find position of element in array of elements with jQuery 如何将 class 添加到索引小于 jquery 的单击元素的所有相似元素? - How to add a class to all similar elements with an index less than the clicked element with jquery? 当在 jquery 每个循环内单击一个元素时,如何删除所有同级元素的 class - How to remove a class of all sibling elements when an element is clicked inside a jquery each loop 在单击时,仅将类应用于单击的元素,而不是将所有元素都应用于同一类 - On click only apply class to element clicked, not all elements with the same class JavaScript遍历类元素并选择除clicked元素之外的所有元素 - JavaScript iterate through class elements and select all except clicked element 如果在特定元素之外单击,请删除具有特定类的所有元素 - Remove all elements with specific class if clicked outside of specific element 如何删除除单击的元素之外的所有其他元素的类 - How to remove a class on all the other elements except on the element that was clicked 对于具有相同类的多个元素,仅影响jquery中的单击元素 - affect only the clicked element in jquery for multiple elements with same class jQuery查找具有特定类的所有元素 - jQuery find all elements with specific class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM