jquery插件公共功能

时间:2010-09-30 16:16:43

标签: javascript jquery plugins

这是我的插件

(function($){
    $.fn.editor = function(options){
        var defaults = {},
        settings = $.extend({},defaults, options);
        this.each(function(){
            function save(){
                alert('voila'); 
            }
        });
    }
})(jQuery);

我想从插件外部调用函数save。我该怎么办?

2 个答案:

答案 0 :(得分:3)

这对我来说效果最好。

(function($){
    $.fn.editor = function(options){
        var defaults = {},
        settings = $.extend({},defaults, options);
        this.each(function(){
            function save(){
                alert('voila'); 
            }
            $.fn.editor.externalSave= function() {
                save();
            }
        });

    }
})(jQuery);

呼叫

$(function(){
    $('div').editor();
    $.fn.editor.externalSave();
});

答案 1 :(得分:1)

例如这样的东西?:

call method

var save = function () {

   var self = this; // this is a element of each

};

(function($){
    $.fn.editor = function(options){
        var defaults = {},
        settings = $.extend({},defaults, options);
        this.each(function(){
           save.call(this) // you can include parameters 
        });
    }
})(jQuery);