2010-09-30 133 views
2

这是我的插件jQuery插件公共职能

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

我想调用函数保存在插件之外。我该怎么做 ?

+1

这是无论如何功能相关的插件,也可以是静态的? – 2010-09-30 16:19:15

+0

该函数是否使用它定义的匿名方法中的任何闭包值进行保存? – 2010-09-30 16:23:00

+0

是的,该函数使用插件闭包中的其他函数和变量。 – 2010-10-01 03:40:33

回答

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

此代码会调用保存在所有编辑器上,我相信这不是所需的效果。 – 2013-08-16 01:18:20

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

我的意图是将函数保存在插件中,并从外部调用它 – 2010-09-30 18:50:22