0

プラグイン内に複数の関数を呼び出したいのですが、何らかの理由でメソッド undefinedを取得します。

メソッドを使用して関数をラップしないと、

関数ステートメントには名前が必要です

  1. ここで何が間違っていますか?
  2. 関数をメソッド var でラップする必要がありますか?

    (function ($) {
    
    var methods = {
    
        // GET TARGET LOCATION AND ANIMATE
        scrollTopPage: function () {
    
            $(htmlBody).animate({
                scrollTop: 0
            }, 'slow');
    
        },
    
        scrollToSect: function (htmlBody) {
    
            $(htmlBody).animate({
    
                scrollTop: $('#page-id').offset().top
            });
    
        },
    
        goToID: function (sectID) {
    
            var htmlBody = 'html,body';
    
            //Scroll to the very top
            if (sectID == 'home') {
    
                methods.scrollTopPage(htmlBody);
    
            } else {
    
    
                methods.scrollToSect(htmlBody);
    
            }
    
        }
    
    } // End the Methods/Functions
    
    
    $.fn.pageScroller = function (methods) {
    
    
        this.click(function (e) {
    
            sectID = $(this).attr('id');
    
                e.preventDefault();
    
                // Undefined Error
                methods.goToID(sectID); // Call the goToID function and pass the sectID variable
    
                    $(this).parent().addClass('current').prev().removeClass('current');
                    $(this).parent().addClass('current').next().removeClass('current');
    
    
        });
    
        $(document).keydown(function (e) {
    
            //To Do Re Use the methods/functions here
    
        });
    
    };
    
    })(jQuery);
    
4

2 に答える 2

1

次の場所で関数を呼び出すのではなく、変数を割り当てています。

 $.fn.pageScroller = function (methods) { ....

関数が から実際に呼び出された$.fn.pageScrollerplugin場合、またはこれを呼び出すものは何でも、パラメーターの名前は になりますがmethods、作成したメソッド オブジェクトにはなりません。代わりに、呼び出し元がパラメーターとして渡すことを選択したものになります。あなたの場合、パラメーターがまったく渡されていないようです。だからmethodsですundefined

この関数にパラメーターを設定しないとmethods、渡されたパラメーターの代わりに関数のコレクションが参照されることになりませんか?

つまり、これを試してみてください。希望methodsはまだアクセス可能です:

 $.fn.pageScroller = function () { ....
于 2013-01-17T13:00:54.617 に答える
0

もう1つの方法は、methodsこのようにプラグイン内にプロパティを持つことです...

(function ($) {

$.fn.pageScroller = function () {

  this.methods = {

    // GET TARGET LOCATION AND ANIMATE
    scrollTopPage: function () {

        $(htmlBody).animate({
            scrollTop: 0
        }, 'slow');

    },

    scrollToSect: function (htmlBody) {

        $(htmlBody).animate({

            scrollTop: $('#page-id').offset().top
        });

    },

    goToID: function (sectID) {

        var htmlBody = 'html,body';

        //Scroll to the very top
        if (sectID == 'home') {

            methods.scrollTopPage(htmlBody);

        } else {


            methods.scrollToSect(htmlBody);

        }

    }

} // End the Methods/Functions



    this.click(function (e) {

        sectID = $(this).attr('id');

            e.preventDefault();

            // Undefined Error
            methods.goToID(sectID); // Call the goToID function and pass the sectID variable

                $(this).parent().addClass('current').prev().removeClass('current');
                $(this).parent().addClass('current').next().removeClass('current');


    });

    $(document).keydown(function (e) {

        //To Do Re Use the methods/functions here

    });

};

})(jQuery);

または、プラグインの作成中にメソッドを渡すこともできます...

于 2013-01-17T13:25:52.747 に答える