将js事件绑定到除1个元素之外的文档

时间:2011-10-05 20:03:44

标签: javascript jquery

基本上我有一个弹出窗口,想要在外面的任何地方点击它时隐藏它,但是一个元素(这是另一个弹出窗口,比如div#AnotherPopup)。然后我点击#AnotherPopup,它不应该隐藏原来的..它现在,因为我有这个:

  $(document).bind('click', methods.hide);

如何将它绑定到文档,除了#AnotherPopup? :)

3 个答案:

答案 0 :(得分:2)

你真的可以,但如果目标元素是methods.hide,你可以在#AnotherPopup内查看,并在做任何事情之前立即退出函数。

答案 1 :(得分:0)

#AnotherPopup的点击处理程序是阻止文档获取点击事件。

$(document).bind("click", function () {
   alert(this.innerHTML) 
});
$('#AnotherPopup').click(function () {return false;});

Example

答案 2 :(得分:-1)

使用unbind你可以这样做:

$(document).bind('click',methods.hide);
$('#AnotherPopup').unbind('click',methods.hide);

详细了解unbind here