为什么这不起作用?我该如何修复它?
<!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");
});
});
关于jquery - 关于jquery绑定(bind)解除绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4121069/