返回嵌套元素的索引(Return index of nested element)
我有两个嵌套的
ul
包含链接。我想在其父级
ul
找到被点击的索引<div id="main_nav"> <ul> <li> <ul> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> </ul> </li> </ul> <ul> <li> <ul> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> </ul> </li> </ul> </div>
我正在使用以下代码:
$("#main_nav ul ul a").click(function () { var index = $('#main_nav ul ul a').index(this); alert(index); });
这似乎返回
parent of the parent
而不是parent of the parent
的a
的索引(我希望这是有意义的)即,单击第二次ul
中的第二个链接将返回索引4而不是1。有人可以解释我哪里出错吗? 我怀疑它很简单,但我无法弄明白。
非常感谢。
I have two nested
ul
which contain links.I want to find the index of the clicked
a
within it's parentul
<div id="main_nav"> <ul> <li> <ul> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> </ul> </li> </ul> <ul> <li> <ul> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> <li><a href="/url/">LINK</link></li> </ul> </li> </ul> </div>
I'm using the following code:
$("#main_nav ul ul a").click(function () { var index = $('#main_nav ul ul a').index(this); alert(index); });
which seems to return the index of the
a
within theparent of the parent
instead of just theparent
(I hope that makes sense) i.e. Clicking the second link in the secondul
returns an index of 4 instead of 1.Could somebody explain where I'm going wrong with this? I suspect it's something simple but I can't figure it out.
Many thanks.
满意答案
您需要获取父
li
元素的索引。 否则,您将获得列表项内锚点的索引,该索引始终为零。$(this.parentNode).index();
You need to get the index of the parent
li
element. Otherwise you are getting the index of the anchor inside the list item, which will always be zero.$(this.parentNode).index();
相关问答
更多Python:测试嵌套列表中某些索引的相等性(Python: Test for equality of certain index in nested lists)
CouchBase Index嵌套元素(CouchBase Index nested elements)
如何在嵌套列表中将索引1处的元素转换为upper()?(how to convert element at index 1 to upper() in a nested list?)
返回向量中索引处的元素(Return element at index in a vector)
返回嵌套元素的索引(Return index of nested element)
Swift访问由`NSIndexPath`指定的嵌套数组元素(Swift accessing nested array element specified by `NSIndexPath`)
使用z-index在其他元素前面创建一个嵌套元素?(Using z-index to make one nested element in front of other element?)
Swift对数组进行排序并返回索引和元素数组(Swift sort an array and return an array of index and element)
在Python中,如何为仲任嵌套元素返回索引列表?(In Python, how do you return a list of indexes for an arbitarily nested element?)
index confusion:嵌套的JavaScript循环 - for循环中的forEach(index confusion: nested JavaScript loops - forEach within a for loop)
相关文章
更多The content of element type "package" must match "...
Hibernate Search(基于version3.4)--第四章Mapping entities to the index structure
顶 lucene--创建index
利用SolrJ操作solr API完成index操作
Solr4.7.2启动时的Index locked for write for core问题分析
Solr4.8.0源码分析(3)之index的线程池管理
[Hadoop 2.2 + Solr 4.5]系列之三:MapReduce + Lucene 生成Index文件
我的架构演化笔记 13:自己动手写Nutch-index插件导入数据到MongoDB
merge solr index &&&&& very import create new core
部署index.html,局域网能访问,远程连接超时
最新问答
更多绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)
如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)
ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)
SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)
宁夏银川永宁县望远镇哪里有修mp5的?
我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)
郑州会计培训班
如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)
在响应图像上叠加网格(Overlay grid on responsive image)
无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!