<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){$("button").click(function(){$("p").hide(1000,function(){alert("The paragraph is now hidden");});});});</script></head><body><button type="button">Hide</button><p>This is a paragraph with little content.</p><p>This is a paragraph with little content.</p><p>This is a paragraph with little content.</p></body></html>callback函数alert()一共提示了三次,我想让它只提示一次,需要如何修改代码?有没有可能通过one()来实现?
3 回答

jeck猫
TA贡献1909条经验 获得超7个赞
$("p").hide(1000,function(){
var n=$("p").index(this);
if(n==0)
alert("The paragraph is now hidden");
});
一、在你这个场景下one()是无法达到你的要求的
二、你可以用n=?来灵活控制什么时候触发alert

月关宝盒
TA贡献1772条经验 获得超5个赞
因为你有三个<p>啊,点击的时候三个都执行了,提示一次的话:
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
});

偶然的你
TA贡献1841条经验 获得超3个赞
其实可以用queue方法,不过有个投机取巧的:你在三个p标签外面套个div,比如设置id="pDiv" 然后 $("#pDiv").hide(1000,function(){ alert("The paragraph is now hidden"); });
- 3 回答
- 0 关注
- 273 浏览
添加回答
举报
0/150
提交
取消