JS callback中alert弹窗出现多次的问题

写了个简单的页面。

单机HIde按钮,隐藏标签为P内容。并callback alert。


隐藏后,callback弹窗出现三次



代码如下:

HTML文件

<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script src="myjs.js"></script>
<title></title>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;/*加载时为隐藏状态*/
}
div.animate{
height: 100px;
width: 100px;
position: relative;
background: red;
}
</style>
</head>
<body>
<p class="set">set it in red.</p>
<button type="button" id="hide">Hide</button>
<button type="button" id="show">Show</button>
<div class="panel">
<p>
Here is going to slip.
</p>
</div>
<p class="flip">click Here</p>
<div class="animate"></div>
<button id="stop">Stop</button>
</body>

</html>


JS文件

//$(document).ready(funciton(){...});jQuery函数写在这里,页面加载后运行$
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide(1000,function(){
alert("already hidden");
});
});
$("#show").click(function(){
$("p").show(1000);
});
$(".flip").click(function(){
$(".panel").slideToggle("slow");
// alert("yes");
});
$(".animate").click(function(){
$(".animate").animate({left:"250px",opacity:"0.4"},2000);
$(".animate").animate({left:"0px",opacity:"1"},2000);
});
$("#stop").click(function(){
$(".animate").stop();
});
});


原因:

$("p").hide(1000,function(){
alert("already hidden");
});

hide函数被调用了三次…因为P标签有三个,解决方法,设置一个id即可。

网上看到许多alert出现多次的问题,可能都是调用多了吧。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值