2012-01-22 15 views
0

プラグイン:関数変数をjqueryプラグインのように呼び出す方法を教えてください。

(function($){ 

    $.fn.myplugin = function(options) { 

    var mymethod = function(){ 
     // I want to be able to access "this" here ('.element') 

     return this; 
    }; 

    return this.each(function() {   
     // $('.element', this).mymethod(); <- how to do this? 
    }); 

    }; 
})(jQuery); 

私はこのようにmymethod呼びたい:

$('.element').mymethod();

これは可能ですか?

は、基本的には

+0

ラッピング関数を取り除き、単に '$'エイリアスを他の場所で使用できるようにしたいのでない限り、 'jQuery.fn.myplugin'に代入することができます。 – davidchambers

答えて

2

jQueryプラグインのオーサリングページをチェックアウトすることをお勧めします。 http://docs.jquery.com/Plugins/Authoring

、このようなものだ

$('#foo').myPlugin('myMethod'); 

あなたがそのような何かを達成するであろう道を行くことによってなり、特定のメソッドを呼び出すための最良の方法(注:すべてのjQueryプラグインのオーサリングサイトから取られた)

(function ($) { 

    var methods = { 
     init : function(options) { 

      // Init function here 

     }, 
     destroy : function() { 

      // Teardown function here 

     } 
    }; 

    $.fn.myPlugin= function(method) { 

if (methods[method]) { 
    return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
} else if (typeof method === 'object' || ! method) { 
    return methods.init.apply(this, arguments); 
} else { 
    $.error('Method ' + method + ' does not exist on jQuery.myPlugin'); 
}  


}; 

})(jQuery); 
1

アルターMyMethodはとてもその直接の延長

$.fn.mymethod = function() { 
    this.hide(); // the .element 
    return this; // continue the daisy chain 
} 

// example usage 
$(document).ready(function() { 
    $("div").mymethod(); 
}); 
...私は、さらに他の関数を呼び出すことができるようにチェーンを維持するために必要

更新X2(タイプミスがありました)!

http://jsfiddle.net/MattLo/2TWMV/1/

+0

私はそれを知っていますが、 'this' =' .element'を作る方法は? – Alex

+0

が例で更新されました。すべての$ .fn。 "methodName"は、jQueryメソッドへの直接の拡張であり、新しいオブジェクトリテラルのリターンはjQuery拡張の一部ではありません。 –

+0

jQueryメソッド内で、 'this'はjQueryコレクションを指し、要素の基本配列ではありません。だから、 '$ .fn.mymethod'のドル記号は不要です。実際には、関数の本体は単に 'return this.hide()'を読むべきです。 – davidchambers

1

さて、あなたはただやるだろう:

$.fn.mymethod = function() { ... return this; } 

jQueryが自動的にそれがthisように呼ばれています要素を渡します。

しかし、このように多くの機能を追加することは悪い習慣と考えられます。だから、ほとんどのプラグインは$.fnに1つの関数を追加し、呼び出すメソッドの文字列引数を取ります。

+0

が更新されました。なぜなら、関数を変数として保持したいのは、私はかなりの数を持っているからです。 – Alex

2

jQueryプラグインのように呼びたい場合は、jQueryプロトタイプ($ .fn)に追加する必要はありません。あなただけこのの参照を持つようにしたい場合はしかし、あなたのセレクタのjQueryの要素は、あなただけapplyを使用することができます。新しいfunctionキーワードがありますたび

(function($){ 

  $.fn.myplugin = function(options) {   

    var mymethod = function(){ 
      // I want to be able to access "this" here ('.element') 

      return this; 
    }; 

    return this.each(function() {         
     mymethod.apply($('.element', this)); 
    }); 

  }; 
})(jQuery); 
2

閉じる、あなただけのthisキーワードを失います。最初に保存してみてください:

(function($){ 

    $.fn.myplugin = function(options) { 

    var that = this; 

    var mymethod = function(_this, dotElem){ 
     // I want to be able to access "this" here ('.element') 
     that.hide().blah()... 

     return this; // this isn't needed as we are going to anyway return `this.each()` 
    }; 

    return this.each(function() {   
     mymethod(this, $('.element')); <- how to do this? 
    }); 

    }; 
})(jQuery); 
関連する問題