如果点击jQuery?

mik*_*ike 6 jquery

使用下面的代码...是否可以说,如果单击li,则执行此操作(用户单击其他位置)执行此操作?如果用户点击列表中的内容,代码将仅使div消失.如果他们点击其他地方我也需要它消失... ?? 谢谢!

.bind('blur', function() {
   $('#search_suggestions_list li').click(function() {
       var selectedString = $(this).text();
       $('#search-box').val(selectedString);
       setTimeout("$('#search_suggestions').fadeOut('fast');", 200);
   })
})
Run Code Online (Sandbox Code Playgroud)

mun*_*nch 8

$(document).click(function(){
   //user clicked anywhere on the page
});

$('#search_suggestions_list li').click(function() {
   //user clicked on the li
   return false;
});
Run Code Online (Sandbox Code Playgroud)

确保返回false,否则也将调用文档单击事件.

  • 通过返回false,您可以暂停冒泡,因此不会运行document.click函数.我可能误解了这个问题,但听起来就像他要求的那样. (7认同)