这是我的插件
(function($){
$.fn.editor = function(options){
var defaults = {},
settings = $.extend({},defaults, options);
this.each(function(){
function save(){
alert('voila');
}
});
}
})(jQuery);
我想从插件外部调用函数save。我该怎么办?
答案 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)
例如这样的东西?:
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);