2010-09-30 181 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