lan*_*nqy 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>
Run Code Online (Sandbox Code Playgroud)
您没有指定什么的绑定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);
});
Run Code Online (Sandbox Code Playgroud)
你可以在这里测试一下.在你的情况下,更容易检查是否<div>隐藏,并且不解除/重新绑定任何东西,如下所示:
$(function() {
$("button").click(function () {
$("div:hidden").show().fadeOut("slow");
});
});
Run Code Online (Sandbox Code Playgroud)