jQuery切换()问题
$('.toggle').toggle(function(){
var ul = $('ul');
ul.fadeOut('fast', function(){
ul.fadeIn('fast').removeClass('off');
ul.addClass('on');
});
}, function(){
ul.fadeOut('fast', function(){
alert('wtf'); // <- never gets here...
ul.fadeIn('fast').removeClass('on');
ul.addClass('off');
});
});
我做错了什么?
我在第二个 fadeOut 回调函数中添加的任何代码都不会被执行...
$('.toggle').toggle(function(){
var ul = $('ul');
ul.fadeOut('fast', function(){
ul.fadeIn('fast').removeClass('off');
ul.addClass('on');
});
}, function(){
ul.fadeOut('fast', function(){
alert('wtf'); // <- never gets here...
ul.fadeIn('fast').removeClass('on');
ul.addClass('off');
});
});
What am I doing wrong??
Any code that I add inside the second fadeOut callback function never gets executed...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
var ul = $('ul');
也放入第二个函数中。Put
var ul = $('ul');
in your second function too.第二个回调中未定义
ul
。更改其范围,使其对两个回调都可见。如果您不希望
ul
处于此范围之外,请将其包装在自调用函数中,或者在toggle( 的每个回调中指定
。ul
)ul
is not defined in the second callback. Change its scope so it is visible to both callbacks.If you don't want
ul
to be in scope outside of this, either wrap it in a self invoking function or specify theul
within each callback oftoggle()
.变量
ul
不存在于第二个函数的作用域中。对于您的情况来说,这实际上是一些不必要的优化,因为以下内容也应该有效:The variable
ul
doesn't exist in the scope of the second function. It's actually a bit of an unneeded optimization in your case, since the following should also work: