从 jQuery 对象本身调用自定义 jQuery 插件

Calling a custom jQuery plugin off the jQuery object itself

本文关键字:jQuery 自定义 插件 调用 对象      更新时间:2023-09-26

我在调用jQuery对象本身的jQuery插件时遇到问题。因此,与其打电话给$(selector).myPlugin(),不如打电话给$.myPlugin。出于某种原因,它告诉我该函数未定义。

这是我的代码:

(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(){}.