jQuery插件公共职能
问题描述:
这是我的插件jQuery插件公共职能
(function($){
$.fn.editor = function(options){
var defaults = {},
settings = $.extend({},defaults, options);
this.each(function(){
function save(){
alert('voila');
}
});
}
})(jQuery);
我想调用函数保存在插件之外。我该怎么做 ?
答
这个最适合我。
(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
此代码会调用保存在所有编辑器上,我相信这不是所需的效果。 – 2013-08-16 01:18:20
答
例如这样的事情?:
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);
+0
我的意图是将函数保存在插件中,并从外部调用它 – 2010-09-30 18:50:22
这是无论如何功能相关的插件,也可以是静态的? – 2010-09-30 16:19:15
该函数是否使用它定义的匿名方法中的任何闭包值进行保存? – 2010-09-30 16:23:00
是的,该函数使用插件闭包中的其他函数和变量。 – 2010-10-01 03:40:33