一种在JQuery中链接事件序列的方法? [英] A way to chain a sequence of events in JQuery?

查看:81
本文介绍了一种在JQuery中链接事件序列的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些事情,一个接一个地要做,不是所有方法都具有回调功能,那么将这些事件链接在一起的最佳方法是什么?

I have a few things going on that need to be done one after the other, not all methods have callback features so what's the best way to go about chaining these events together?

我有这样的东西

关闭滑块>删除旧内容>追加新内容>重新打开滑块

Close slider > Delete old content > Append new content > Re-open slider

我是JQuery的新手,所以这似乎很明显,但我感谢您的帮助!

I'm new to JQuery so this may seem obvious but I appreciate any help!

推荐答案

您可以使用队列和显式定义的队列,但是在这种情况下,您可以使用

You can use queues and explicitly defined queue, but in this case you can just make use of a call back with .slideUp().

由于可以链接动画以将其添加到默认队列,因此您只需链接 .slideDown () .

Since animations can be chained to add them to the default queue, you can just chain .slideDown() right after the .slideUp().

$(sliderSelector).slideUp(function() { 
    // delete and add stuff
}).slideDown();

jsFiddle示例

jsFiddle example

如果您正在使用自定义的向上滑动和向下滑动功能而没有回叫,那么我也不知道您是否也将自动排队功能添加到了链接中,因此您可以使用快速创建队列. > .queue() :

If you are using custom slide up and slide down functionality with no call back, then I don't know if you have automatic queueing functionality added into the chaining either, so you can just create a queue on the fly using .queue():

$(this).queue(function() {
      // Stuff to do
    // ...
      // Dequeue
    $(this).dequeue; 
});

因此您的整个代码将是:

So your whole code would be:

    $(this).queue(function() {
        $(this).customUp();
        $(this).dequeue();
    }).queue(function() {
        // Delete & add stuff
        // ...
        $(this).dequeue();
    }).queue(function() {
        $(this).customDown();
        $(this).dequeue();
    });

jsFiddle示例

jsFiddle example

这篇关于一种在JQuery中链接事件序列的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆