0

単に私は次のものを持っています:

jQuery.fn.X = function(){
    var elements = jQuery(this);
    elements.click(function(){
        var self = jQuery(this);
        //elements here is not defined why?
    });

elementsクロージャー変数として取る必要があるのに、なぜクリック関数で定義されていないのですか?

4

1 に答える 1

3

これは、jQuery プラグインを作成するための正しいアプローチです。

jQuery.fn.X = function () {
    // here, "this" will be a jQuery object containing all elements you matched
    // with X(). You must return that object.
    return this.click(function () {
        // here, "this" will be a DOM element. You don't have to return it.
        var self = jQuery(this);
        // ...
    });
});

メソッドチェーンを機能させ続けるには、jQuery を返す必要があります。

于 2013-06-30T05:42:13.420 に答える