如何将JS对象传递给jQuery函数.queue()

How to pass JS Object to jQuery function .queue()

本文关键字:函数 jQuery queue JS 对象      更新时间:2023-09-26

我在开发中使用OOP和jQuery。我曾经把我当前的对象this传递给jQuery函数,如下所示:

$(myElement).live('click', this, function(el){
  // I can access to my JS object using el.data
});

但我找不到如何用jQuery函数.queue()做类似的事情。有可能吗?

编辑

我给你一个我想使用.queue():的上下文

CAPTIVEA.widget.Message = {
    /**
     * Displays generated message on the screen
     * @method display
     * @public
     */
    display: function() {
        // Display Message
        $('.message')[this.effects.show](this.effects.duration, function(){
            $(this).show();
            $('.message span').show();
            $('.message').children().show();
        });
        if (this.autoHide)
        {   // Remove message after delay
            $('.message').data('objMessage', this);
            $('.message').delay(3000).queue(function(el){
                $(this).data('objMessage').close();
            });
        }
    },
    /**
     * Removes generated message from the screen
     * @method close
     * @public
     */
    close: function() {
        $('.message')[this.effects.hide](this.effects.duration, function(){
            $(this).remove();
        });
    }
};

"有可能吗?"

没有。live()方法是一种事件处理方法,您正在设置事件对象数据。回调中的第一个参数是事件对象。

我不知道this代表什么,但我感觉你滥用了事件数据。

queue()方法与事件处理无关。您向它传递一个添加到队列中的函数。它的第一个参数将引用一个释放队列的函数。

您仍然可以访问当前对象。请在此处查看我的jsfiddle:http://jsfiddle.net/9upJB/

除此之外,其他人所指出的是正确的。live()(应该使用btw停止)是一个事件。queue()是一个方法,与事件无关。

访问对象时,这将作为回调执行(请查看jQuery文档)。

您首先将实例保存为元素数据,并在jquery的队列函数中使用它;

$("#el").data("instance", this);

en之后在功能中使用

$("#el").queue(function(){
    var instance = $(this).data("instance");
    //do whatever you want
});

您不向元素传递queue()函数,而是在匹配的元素上执行该函数。

$('yourelement').queue();