0

我有一些本地函数放在插件的方法中,我想知道我是否可以从外部访问插件方法的函数?

这是我的代码,

$(document).ready(function(){
   $.fn.hilight.init.plugins();
});

(function ($) {

    var pluginName = 'hilight';

    var methods = {

        init: function(options){

            var base = this;

                    // Method's function.
            base.plugins = function() {
                alert("an internal function");
            }

            var o = $.extend(true, {}, $.fn.hilight.defaults, options );

        }
    }

    $.fn[pluginName] = 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 );  // always change 'init' to something else if you different method name.
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.plugin' );
        }

        return this; 

    };

    $.fn[pluginName].defaults = {
        foreground: "red",
        background: "yellow"
    }


}( jQuery ));

但我当然得到一个错误,

TypeError: $.fn.hilight.init 未定义 [中断此错误]

$.fn.hilight.init.plugins();

有什么想法可以实现吗?

jsfiddle

4

0 回答 0