2

私は最初の jQuery プラグインを作成しようとしています。jQuery プラグインのオーサリングを読んでいます。それが本当に強調していることの 1 つは、名前空間の代わりにメソッドを使用することです。また、そのガイドのデータを使用してメソッド間で変数をバウンスしようとしていますが、機能していません。いくつかのデータを作成し、それを別のメソッドに渡して更新し、別の場所に送信する必要があります。

(function($) {

    var methods = {
        init : function(settings) {

            return this.each(function(){

                var x = 3; 
                var object = $(this);

                // Bind variables to object
                $.data(object, 'x', x);

                $(object).bbslider('infoParse');

                var y = $.data(object,'y');
                alert(y);

            }); // End object loop

        }, // End init
        infoParse : function() { 
            var object = $(this);
            var x = $.data(object,'x');

            alert(x);

            var y = 10;
            $.data(object,'y',y);
        }, // End infoParse
    }; // End method


    $.fn.bbslider = 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.bbslider' );
        }       
    }; // End slider

})(jQuery); 

ここに私が何を意味するかを示すjsFiddleがあります:http://jsfiddle.net/wRxsX/3/

メソッド間で変数を渡すにはどうすればよいですか?

4

1 に答える 1

2

このhttp://jsfiddle.net/wRxsX/6/が大丈夫かどうかを確認してください。

要素.データ(k,v)

$.data は要素で使用する必要があります

http://api.jquery.com/data/

Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

于 2013-01-26T04:09:05.740 に答える