【问题标题】:How to pass JS Object to jQuery function .queue()如何将 JS 对象传递给 jQuery 函数 .queue()
【发布时间】:2012-02-01 14:22:27
【问题描述】:

我在开发中使用 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();
        });
    }
};

【问题讨论】:

  • 你的意思是像$(myElement).queue(function(el){});?因为这是可能的......
  • this在你的代码中代表什么类型的对象?
  • @am not i am :它是对象文字。我给你的代码示例在这个对象的一个​​方法中
  • 你如何使用.queue()。该方法只对碰巧从中调用它的任何 jQuery 对象进行操作。您是否需要与特定元素或元素集相关联的数据?如果有,你考虑过$.data()吗?
  • @am not i am : 我已经编辑了我的帖子以提供背景信息。

标签: javascript jquery oop


【解决方案1】:

“有可能吗?”

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      在函数中使用之后

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

      【讨论】:

        【解决方案4】:

        您没有将queue() 函数传递给元素,而是在匹配的元素上执行该函数。

        $('yourelement').queue(); 
        

        【讨论】:

          猜你喜欢
          • 2015-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-30
          • 2015-10-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多