【问题标题】:Calling a custom jQuery plugin off the jQuery object itself从 jQuery 对象本身调用自定义 jQuery 插件
【发布时间】:2012-06-04 12:16:48
【问题描述】:

我无法从 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);

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

【问题讨论】:

    标签: javascript jquery jquery-plugins


    【解决方案1】:

    看那个问题:in jQuery what is the difference between $.myFunction and $.fn.myFunction?

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

    【讨论】:

    • 还有一个问题……有没有推荐的方法从init 方法中调用showByCode 方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多