我的结果是ui-id-1
。但是,如果想要id
锚标记ui-id-5
和其他人。
$(".ui-corner-all").click(function() {
alert(this.id);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" id="ui-id-1" tabindex="0" style="display: none; top: 297px; left: 746px; width: 572px;">
<li class="ui-menu-item" role="presentation">
<a id="ui-id-5" class="ui-corner-all" tabindex="-1">Kamran Khan</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-6" class="ui-corner-all" tabindex="-1">Muddassir</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-7" class="ui-corner-all" tabindex="-1">Dr. M. A. al-mamun</a>
</li>
</ul>
&#13;
答案 0 :(得分:2)
将您使用的选择器更改为仅适用于a
元素
$("a.ui-corner-all").click(function() {
alert(this.id);
});
$("a.ui-corner-all").click(function() {
alert(this.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" id="ui-id-1" tabindex="0" style="/*display:none*/top: 297px; left: 746px; width: 572px;">
<li class="ui-menu-item" role="presentation">
<a id="ui-id-5" class="ui-corner-all" tabindex="-1">Kamran Khan</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-6" class="ui-corner-all" tabindex="-1">Muddassir</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-7" class="ui-corner-all" tabindex="-1">Dr. M. A. al-mamun</a>
</li>
</ul>
答案 1 :(得分:1)
为li
点击编写jquery函数。
$("li").click(function () {
alert($(this).find('a').attr('id'));
});
答案 2 :(得分:1)
id
元素没有li
。另外,要获取当前的jquery对象,需要$(this)来使用其他jquery方法。您需要将活动从ul
委托给li
并使用find
查找具有ID属性的a
标记
$(".ui-corner-all li").click(function() {
alert($(this).find('a').attr('id'));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" id="ui-id-1" tabindex="0">
<li class="ui-menu-item" role="presentation"><a id="ui-id-5" class="ui-corner-all" tabindex="-1">Kamran Khan</a></li>
<li class="ui-menu-item" role="presentation"><a id="ui-id-6" class="ui-corner-all" tabindex="-1">Muddassir</a></li>
<li class="ui-menu-item" role="presentation"><a id="ui-id-7" class="ui-corner-all" tabindex="-1">Dr. M. A. al-mamun</a></li>
</ul>
&#13;
答案 3 :(得分:1)
$("ul li").click(function() {
alert($(this).find('a').attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" id="ui-id-1" tabindex="0">
<li class="ui-menu-item" role="presentation"><a id="ui-id-5" class="ui-corner-all" tabindex="-1">Kamran Khan</a></li>
<li class="ui-menu-item" role="presentation"><a id="ui-id-6" class="ui-corner-all" tabindex="-1">Muddassir</a></li>
<li class="ui-menu-item" role="presentation"><a id="ui-id-7" class="ui-corner-all" tabindex="-1">Dr. M. A. al-mamun</a></li>
</ul>
答案 4 :(得分:-1)
$("#ui-id-1").on('click', 'a', function () {
alert($(this).find('a').attr('id'));
});
<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" id="ui-id-1" tabindex="0">
<li class="ui-menu-item" role="presentation"><a id="ui-id-5" class="ui-corner-all" tabindex="-1">Kamran Khan</a></li>
<li class="ui-menu-item" role="presentation"><a id="ui-id-6" class="ui-corner-all" tabindex="-1">Muddassir</a></li>
<li class="ui-menu-item" role="presentation"><a id="ui-id-7" class="ui-corner-all" tabindex="-1">Dr. M. A. al-mamun</a></li>
</ul>