2010-11-08 50 views
2

爲什麼這不起作用?我該如何修復它?關於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> 
+0

你一直是SO的用戶1年,仍然沒有代碼格式? – RPM1984 2010-11-08 03:41:42

+0

什麼不起作用?有沒有錯誤? – rahul 2010-11-08 03:41:48

回答

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); 
}); 

You can test it out here。在你的情況下,雖然,它更容易只是檢查<div>是隱藏的,也不要UN /重新綁定任何東西,就像這樣:

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

You can test that version here