关于 jquery 绑定和取消绑定

发布于 2024-10-01 05:59:42 字数 707 浏览 0 评论 0原文

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

<!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>

why this does't work? and how can I fixed it?

<!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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2024-10-08 05:59:42

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

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

您可以在此处进行测试。不过,就您而言,更容易检查

是否隐藏,并且不要取消/重新绑定任何内容,如下所示:

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

您可以在此处测试该版本

You aren't specifying what to bind the click event to with $("button").bind("click"); (it doesn't make any assumptions there, it just silently binds nothing). You can do this with a named function you store though, for example:

$(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. In your case though, it's easier to just check if the <div> is hidden, and don't un/re-bind anything, like this:

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

You can test that version here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文