取消绑定(bind)click to div。我有两个div。我想解除点击事件与内部div的绑定(bind)
当页面加载时,我想解除点击事件与三 Angular 形
的绑定(bind)。
我的 JQuery 代码:
$(".wrapper").unbind();
$(".triangles").unbind();
$(".triangles" ).click(function(){ //it is still clickable
alert("hello u clicked me");
});
<div class="wrapper">
<div class="triangles">
</div>
<div class="triangles">
</div>
<div class="triangles">
</div>
</div>
最佳答案
在需要时使用 .off()
取消绑定(bind)事件。
示例:
$(".triangles").off("click");
另一个例子:这里第一次可以点击,第二次就不能点击
$(".triangles" ).click(function(){
alert("hello u clicked me");
$(this).off('click'); //after this, $(".triangles") will not longer be clickable
});
尝试一下:)
让我解释一下,你的代码中做错了什么
$(".wrapper").unbind(); //unbinded all the events
$(".triangles").unbind(); //unbinded all the events
$(".triangles" ).click(function(){ //binded another click event
alert("hello u clicked me"); //So of course it will be clickable
});
关于javascript - 取消绑定(bind)click to div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468741/