函数在 fadeOut() 之前触发;完成了
我是 javascript/jQuery 的新手,这真的让我难住了。
我在这里想要实现的是
- 在切换a#sameDayTab时jquery将查找.changeAlert和fadeOut它是容器div,当再次切换时div将淡入(这效果很好。)
- 每个切换还将调用一个函数来告诉我页面上存在多少个 .changeAlert,并在一个范围内适当更新数量。问题是,当我第一次单击切换的锚点时,可见数应该为 0,因为 .changeAlert 已被 fadeOut 隐藏,而不是返回页面加载时存在的类数,无论激活切换多少次,该值都不会改变。
非常感谢任何帮助。
function totalNumFares ()
{
var n = $('.changeAlert:visible').size();
$('.numFares').replaceWith('<span class=\"numFares\">'+ n +'</span>');
}
totalNumFares();
//Toggle On/off Same Day Connections
$('a#sameDayTab').toggle(function() {
$('.changeAlert').parent().parent().parent().parent().parent().fadeOut();
totalNumFares();
},function(){
$('.changeAlert').parent().parent().parent().parent().parent().fadeIn();
totalNumFares();
});
I'm new to javascript/jQuery this really has me stumped.
What I'm trying to achieve here is
- On toggling a#sameDayTab jquery will look for .changeAlert and fadeOut it's container div, when toggled again the div will fade in (this works well.)
- Each toggle will also call a function that tells me how many .changeAlert's are present on the page and updates the number appropriately in a span. The problem is when I first click the toggled anchor the number of visible should be 0 as the .changeAlert has been hidden by fadeOut instead it returns the number of classes present on page load this value never changes no matter how many times the toggle is activated.
Any help greatly appreciated.
function totalNumFares ()
{
var n = $('.changeAlert:visible').size();
$('.numFares').replaceWith('<span class=\"numFares\">'+ n +'</span>');
}
totalNumFares();
//Toggle On/off Same Day Connections
$('a#sameDayTab').toggle(function() {
$('.changeAlert').parent().parent().parent().parent().parent().fadeOut();
totalNumFares();
},function(){
$('.changeAlert').parent().parent().parent().parent().parent().fadeIn();
totalNumFares();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将其作为
.fadeOut()
回调运行,例如this:目前它在开始褪色后立即执行,但它是
:visible
直到它完成褪色,所以要算你想要的,您需要在.fadeOut()
完成后(即回调运行时)更新它。另外,您可以将
.parent()
链替换为单个.closest(selector)
调用(如果您有相关课程)您可以将父级用作选择器。You need to run it as the
.fadeOut()
callback, like this:Currently it executes immediate after it starts fading, but it's
:visible
until it finishes fading, so to have that count what you want, you need to update it after the.fadeOut()
has finished, which is when the callback runs.Also, you can likely replace that
.parent()
chain with a single.closest(selector)
call if you have a class on that parent you can use as a selector.您需要将其设置为回调:
http://api.jquery.com/fadeOut/
You need to set it as a callback:
http://api.jquery.com/fadeOut/