锋利的JQuery一书中对toggle方法的讲解停留在1.9版本以前,现在这个方法已经删除了切换绑定事件的用途,于是我想尝试一下几种不同的替代方法:
<style>
div:nth-of-type(1){
width: 200px;
height: 200px;
background-color: aqua;
float: left;
display: block;
}
div:nth-of-type(2){
width: 200px;
height: 200px;
background-color: tomato;
margin-left: 200px;
}
</style>
</head>
<body>
<div id="show-box"></div>
<div id="fade-box"></div>
<script>
$(function(){
$("#show-box").click(function(){
if($("#show-box").css("display")=="block"){//is(":hidden")
$(this).hide(300);
}else{
$(this).show(300);
}
});
var i = 0;
$("#fade-box").click(function(){
if(i == 0){
$(this).fadeOut(300);
i = 1;
}else if(i == 1){
$(this).fadeIn(300);
i = 0;
}
console.log(i);
})
})
</script>
两种方法都无法工作:都是只能消失而不能再次出现,想请问一下各位大佬是哪里出了问题...已经查阅了一些博文,用的也多是这两种方法...