2

为什么这不起作用?我该如何解决?

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

1 回答 1

5

您没有指定将事件绑定到什么(它没有在那里做出任何假设,它只是默默地绑定什么)。您可以使用您存储的命名函数来执行此操作,例如: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);
});

你可以在这里测试一下。但是,在您的情况下,检查是否隐藏更容易<div>,并且不要取消/重新绑定任何内容,如下所示:

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

您可以在此处测试该版本

于 2010-11-08T03:41:52.910 回答