在jquery插件中调用多个函数

在jquery插件中调用多个函数,jquery,function,plugins,Jquery,Function,Plugins,我喜欢在插件中调用多个函数,但由于某些原因,我没有定义方法 如果我不使用方法包装我得到的函数 函数语句需要一个名称 我做错了什么 我需要用var方法包装函数吗 (function ($) { var methods = { // GET TARGET LOCATION AND ANIMATE scrollTopPage: function () { $(htmlBody).animate({ scrollTop: 0

我喜欢在插件中调用多个函数,但由于某些原因,我没有定义方法

如果我不使用方法包装我得到的函数

函数语句需要一个名称

  • 我做错了什么
  • 我需要用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);
    

  • 您正在分配变量,而不是在以下位置调用函数:

     $.fn.pageScroller = function (methods) { ....
    
    我猜这会导致在实际从
    $.fn.pageScrollerplugin
    调用函数时,或者无论调用什么,该参数都将命名为
    methods
    ,但不是您创建的methods对象。相反,它将是调用程序选择作为参数传递的任何内容。在您的情况下,似乎根本没有传递任何参数。这就是为什么
    方法
    未定义的

    如果没有为此函数设置参数,是否会导致
    方法
    引用函数集合而不是传递的参数

    Ie:试试这个,希望
    方法
    仍然可以访问:

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

    另一种方法是像这样在插件中拥有
    方法
    属性

    (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);
    
    或者你也可以在创建插件时传递方法