函数在 fadeOut() 之前触发;完成了

发布于 2024-08-31 23:35:01 字数 870 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-09-07 23:35:01

您需要将其作为 .fadeOut() 回调运行,例如this:

$('.changeAlert').parent().parent().parent().parent().parent().fadeOut(totalNumFares);

目前它在开始褪色后立即执行,但它是:visible直到它完成褪色,所以要算你想要的,您需要在 .fadeOut() 完成后(即回调运行时)更新它。

另外,您可以将 .parent() 链替换为单个 .closest(selector) 调用(如果您有相关课程)您可以将父级用作选择器。

You need to run it as the .fadeOut() callback, like this:

$('.changeAlert').parent().parent().parent().parent().parent().fadeOut(totalNumFares);

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.

天暗了我发光 2024-09-07 23:35:01

您需要将其设置为回调:

$(function(){
    // You can set duration to whatever you like
    $("#item").fadeOut(duration, function(){
        totalNumFares();
    });
});

http://api.jquery.com/fadeOut/

You need to set it as a callback:

$(function(){
    // You can set duration to whatever you like
    $("#item").fadeOut(duration, function(){
        totalNumFares();
    });
});

http://api.jquery.com/fadeOut/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文