$(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 イベント ハンドラーもバインド解除されました。これは私にとって問題です。