【问题标题】:jquery plugin boilerplate elementjquery插件样板元素
【发布时间】:2012-07-10 03:49:54
【问题描述】:

我正在尝试使用 jquery-boilerplate-v3.1 开发插件,但在“this”和“$(this)”之间感到困惑

    in the plugin 
    ...
    $.fn[pluginName] = function ( options ) {
    //alert($(this).toSource());
    return this.each(function () {
        if (!$.data(this, 'plugin_' + pluginName)) {
            $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
        }
    });
};

似乎new Plugin(this, options) 没有返回 Plugin.prototype 上下文中的元素。

相反,我已将其修改为 Plugin($(this), options)

eg.
   $(function(){
     $('#container').myPlugin();
   });

不使用 $(this) 作为参数,我无法访问插件中的 this.element,.toSource() 返回空对象({})

通过修改为 $(this) 或如何使用 this 参数访问#container,我的方法是否正确。

TIA。

【问题讨论】:

  • 似乎这个世界上有很多“样板”。我上面说的是从[link]jqueryboilerplate.com下载的盘子

标签: jquery boilerplate


【解决方案1】:

在插件中,this 指的是您应用插件的 jQuery 对象,但在 this.each() 的回调中,this 指的是 jQuery 对象中的每个单个 DOM 元素。

所以,是的,您需要$(this) 来获取包含循环内该元素的 jQuery 对象。

【讨论】:

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