我正在使用javascript / jquery和html获取选定的锚标记值并在html的UL元素鼠标悬停时禁用其click事件 我尝试下面的代码,但目标没有实现。我该怎么办? 提前致谢
HTML
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all test1"
role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1;
top: -15px; left: 115px; display: none; position: relative; width: 300px;">
<li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">City
Name</a></li><li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">
<br>
</a></li>
<li class="ui-menu-item" role="menuitem"><a class="ui-corner-all" tabindex="-1">
<br>
</a></li>
</ul>
的Javascript
<script type="text/javascript">
$(".ui-autocomplete").hover(function () {
$(".ui-autocomplete li").find('a:contains(City Name)').click(function () {
$(this).unbind();
});
</script>
答案 0 :(得分:0)
您的代码出现问题:
$this = $(this);
代替$(this);
,因为您引用了父元素。我不确定你想要达到的目标,但这将是朝着正确方向迈出的一步。
$(function () {
$(".ui-autocomplete").hover(function () {
$this = $(this);
$(".ui-autocomplete li:first-child").find('a').click(function () {
$(this).unbind();
});
});
});
&#13;
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all test1" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: -15px; left: 115px; position: relative; width: 300px;">
<li class="ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">City Name</a>
</li>
<li class="ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1"></a>
</li>
<li class="ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1"></a>
</li>
</ul>
&#13;