关于javascript:从jQuery对象本身调用自定义jQuery插件

Calling a custom jQuery plugin off the jQuery object itself

我无法从jQuery对象本身调用jQuery插件。因此,我不想调用$(selector).myPlugin(),而是要调用$.myPlugin。由于某种原因,它告诉我该函数未定义。

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(function ($) {

    var _current = null;

    var methods = {
        init: function (options) {
            _current = $('.rfgQuestionsWrapper').filter(function () {
                return $(this).css('display') != 'none';
            }).first();
            console.log(_current);
            $('.gaugeTitle').click(function (e) {
                var rfgCode = $(this).parent().find('.gaugeWrapper').attr('id');
                console.log(rfgCode);
                showByCode(rfgCode);
                return false;
            });
        },
        showByCode: function (rfgCode) {
            var target = $.utilities.filterById('.rfgQuestionsWrapper', rfgCode);
            console.log(target);
            _current.hide();
            target.show();
        }
    };

    $.fn.navigationManager = 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.tooltip');
        }
    };

})(jQuery);

我肯定做错了,因为这是我第一次这样调用插件...有什么建议吗?


看看这个问题:在jQuery中,$。myFunction和$ .fn.myFunction有什么区别?

基本上而不是$.fn.navigationManager = function(){},而是编写$.navigationManager = function(){}