jquery - 关于jquery绑定(bind)解除绑定(bind)

标签 jquery bind unbind

为什么这不起作用?我该如何修复它?

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
            $("button").unbind("click");
            $("div").show().fadeOut("slow",function(){
                $("button").bind("click");
            });
            })
        })
    </script>
    <style>
        div{width:600px;height:600px;display:none;background:red;}
    </style>
</head>
<body>
    <button>test</button>
    <div></div>
</body>
</html>

最佳答案

您没有指定什么来绑定(bind)click事件与 $("button").bind("click"); (它不会在那里做出任何假设,它只是默默地不绑定(bind)任何东西)。您可以使用存储的命名函数来执行此操作,例如:

$(document).ready(function() {
  function clickHandler() { 
    $("button").unbind("click");
    $("div").show().fadeOut("slow",function() {
      $("button").bind("click", clickHandler);
      //or: $("button").click(clickHandler);
    });
  }
  $("button").click(clickHandler);
});

You can test it out here 。但就您而言,检查 <div> 是否更容易是隐藏的,并且不要取消/重新绑定(bind)任何内容,如下所示:

$(function() {
  $("button").click(function () { 
    $("div:hidden").show().fadeOut("slow");
  });
});

You can test that version here .

关于jquery - 关于jquery绑定(bind)解除绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4121069/

相关文章:

javascript - 为什么通过 Ajax 而不是通过常规 Http 请求发送文件对象数组是安全的?

javascript - css,jquery调整可拖动div之间的线

AngularJS 观察者未绑定(bind)/观察

java - 在 Java 中创建热键

jquery - 删除/解除绑定(bind)悬停在 anchor 上

jQuery:bind() 的正确方法;并解除绑定(bind)();通过鼠标悬停、鼠标移出并单击

javascript - $.deferred() 链中的第一个 ajax 调用永远不会到达那里,但第二次单击按钮可以正常工作

JavaFX 不同类型列表的bindContent双向

Jquery,取消绑定(bind)鼠标滚轮事件,然后在操作完成后重新绑定(bind)它?

javascript - 禁用 Javascript 中的 dom 更改事件?