[英]Get parent <li> item with jQuery
I am trying to get the parent <li>
when I click on a specific <li>
item. 当我点击特定的
<li>
项目时,我正在尝试获取父<li>
。
http://jsfiddle.net/doonot/GjbZk/ http://jsfiddle.net/doonot/GjbZk/
So let's say I click on submodule 1
, I get the clicked ID with $(this).attr('id');
所以,假设我点击
submodule 1
,我得到点击的ID与$(this).attr('id');
How can I now get the ID of the parent <li>
which is in this case module 1
? 我现在如何获得父
<li>
的ID,在这种情况下是module 1
?
Please note that my tree is quite big, so it has to be flexible. 请注意我的树很大,所以它必须灵活。
Thanks a lot, much appreciate your answer. 非常感谢,非常感谢您的回答。
You can use the closest
method to get the first ancestor that matches a selector: 您可以使用
closest
方法来获取与选择器匹配的第一个祖先:
var li = $(this).closest("li");
From the jQuery docs: 来自jQuery文档:
Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
获取与选择器匹配的第一个元素,从当前元素开始并逐步向上遍历DOM树。
var parentModule = $('this') //the selector of your span
.parents('li:eq(1)') //get the next li parent, not the direct li
.children('.rightclickarea:first') //get the first element, which would be the span
.attr('id') //get the span id
var parent = $(this).closest("li");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.