【问题标题】:How to chain jquery plugin option with regular jquery methods?如何使用常规 jquery 方法链接 jquery 插件选项?
【发布时间】:2013-06-20 20:52:43
【问题描述】:

我正在制作一个名为 myPlugin 的插件(它是一个对象),并且以这种方式将选项对象作为属性添加到其中(希望我做对了):

$.fn.myPlugin.options = {type: 'slideToggle'}

现在,在 myPlugin 对象的方法中,我将如何更改:

$(this).next().slideToggle(200);

所以它从选项中获取 slideToggle,所以它不是硬编码的?

类似:

$(this).next().myPlugin.options.type(200);

【问题讨论】:

标签: jquery jquery-plugins


【解决方案1】:

您可以使用括号表示法,因为它允许变量:

$(this).next()[self.options.type](200);
                 // ^^^ string that matches jQuery method, like slideToggle etc

【讨论】:

    【解决方案2】:

    要链接,您需要在插件中返回 jQuery 对象,以便将其传递给下一个方法。

    像这样:

     return this.each(function(){
            // element-specific code here
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多