我通过 $(document).bind() 将事件处理程序绑定(bind)到页面的每个元素。
有人知道如何为特定元素取消绑定(bind)此处理程序吗?
这是我的示例代码,但它不能按我想要的方式工作:
<html><head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"> </script>
</head>
<body>
<div id="mid" style="cursor:pointer" >click me</div>
<div>sample</div>
<script type="text/javascript">
var fun = function() {
alert('default handler');
};
$(document).bind('click.custom', fun);
//trying to unbind the handler but it remains there
$("#mid").unbind('click.custom');
</script>
</body>
</html>
考虑丹尼尔在这里的回答又是这个问题:
var ff = function() {
alert('additional');
}
$(document).bind('click.custom', fun);
$(document).bind('click.custom2', ff);
$("#mid").bind('click.custom', false);
此处 fun 事件处理程序与 #mid 元素完全解除绑定(bind),但 ff 事件处理程序也未绑定(bind),这对我来说是一个问题。
最佳答案
这就是所谓的事件冒泡。嵌套元素的事件将传递给父元素。
您应该绑定(bind)
到#mid
并从该处理程序返回false
。
关于jquery - 无法取消绑定(bind)到 $(document) 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5319660/