我需要找到所有锚标签,直到特定元素的父级。示例在这里 -
<li id="li4">
<a id="ancHideShow4" onclick="hide('4');" href="#" style="font-weight:bold;">SECURITY</a>
<ul id="ul4" style="display: block; margin-left: -20px; list-style: none;">
<img onclick="hide('5');" id="img5" src="../../images/asd.gif" class="Hover">
<li id="li5">
<a id="ancHideShow5" onclick="hideshow('5');" href="#" style="">GROUP</a>
<ul id="ul5" style="">
<div class="QWER"></div>
<li id="leafli6">
<a id="ancRedirect312" onclick="Redirect('#')" href="#"><span>XYZ</span></a>
</li>
</ul>
</li>
</ul>
</li>
所以,如果我点击 - &gt; &#34; ancRedirect312&#34;
应找到这些ID - &gt; ancRedirect5,ancRedirect4
帮助我!!
答案 0 :(得分:1)
您可以找到所有父li
元素,然后找到其a
子元素
$('a').click(function() {
var ids = $(this).closest('li').parents('li').children('a').map(function() {
return this.id
}).get();
alert(ids)
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li id="li4">
<a id="ancHideShow4" onclick="hide('4');" href="#" style="font-weight:bold;">SECURITY</a>
<ul id="ul4" style="display: block; margin-left: -20px; list-style: none;">
<img onclick="hide('5');" id="img5" src="../../images/asd.gif" class="Hover" />
<li id="li5">
<a id="ancHideShow5" onclick="hideshow('5');" href="#" style="">GROUP</a>
<ul id="ul5" style="">
<div class="QWER"></div>
<li id="leafli6">
<a id="ancRedirect312" onclick="Redirect('#')" href="#"><span>XYZ</span></a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
&#13;