简体   繁体   中英

jquery plugin public function

This is my plugin

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

I want to call function save from outside the plugin. How can I do it ?

this works best for me.

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

    }
})(jQuery);

call

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

for example something like this?:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM