我通过 $(document).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 元素完全解除绑定,但 ff 事件处理程序也解除绑定,这对我来说是个问题。