javascript - 取消绑定(bind)click to div

标签 javascript html bind jquery

取消绑定(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/

相关文章:

html - 在元标记中使用时间戳而不是正常日期格式是否有效?

javascript - 从 AJAX POST 响应获取和存储 cookie(来自 Set-Cookie)

javascript - 如何在 JavaScript 中更改字体大小?

javascript - Firefox 扩展中的 __defineGetter__ 未定义

javascript - 根据其中的内容设置 p 元素的高度

html - 如何将我的元素符号图像与我的 <li> 内容对齐?

mysql - 使用 DLZ "/usr/bin/ld: cannot find -lmysqlclient"编译 Bind 时出现 CentOS 错误

javascript - 绑定(bind)就绪功能和调整大小事件

javascript - Ajax 是不是将变量传递给另一个页面?

javascript - 将触摸事件仅绑定(bind)到类中的元素而不是其子元素