1

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)に変更して正しい方法を実行していますか、またはパラメーターを使用して#containerにアクセスするにはどうすればよいですかthis

TIA。

4

1 に答える 1

2

プラグインでは、プラグインを適用するjQueryオブジェクトを参照しますが、thisのコールバック内では、jQueryオブジェクト内の各単一のDOM要素を参照します。this.each()this

$(this)したがって、はい、ループ内にその要素を含むjQueryオブジェクトを取得する必要があります。

于 2012-07-10T03:59:21.487 に答える