2013-03-06 37 views
0

我正在尋找一種方法來爲我的插件中的方法添加一個額外的參數。在一個插件中爲方法添加一個參數

這個例子我使用了一個更新方法,但它需要一個額外的參數來告訴要更新什麼。

//插件封裝

;(function($, window, document, undefined){ 

     var pluginName = 'myPlugin01'; 

     function Plugin(element, options){ 

      // vars here 
     }; 

     Plugin.prototype = { 

      init: function(){ 

      // init code here 

      }, 
      update: function(param){ 

       // need the param value in this method 
       if(param == 'bottom'){ 
        alert('bottom it is...') 
       }else{ 
        alert('top it is...') 
       } 

      }, 
     }; 

     $.fn[pluginName] = function(option) { 
      return this.each(function() { 
       var $this = $(this); 
       var data = $this.data(pluginName); 
       var options = typeof option == 'object' && option; 
       if (!data){ 
       $this.data(pluginName, (data = new Plugin(this, options))) 
       } 
       if (typeof option == 'string'){ 
        data[option](); 
       } 
      }); 
     }; 

     $.fn[pluginName].defaults = { 
      option1: true 
     }; 

    })(jQuery, window, document); 

//我要如何使用它

$('.element').myPlugin('update','bottom'); 

回答

0

不知道你正在嘗試做的,但你可以到$.fn[pluginName]和使用添加額外的ARG它爲update或任何方法

//Added additional arg 
$.fn[pluginName] = function(option, mydata) { 

然後在調用部分,

data[option](mydata); 
+0

是這就是它,非常感謝! – user759235 2013-03-06 18:23:36

+0

@ user759235如果回答了這個問題,請不要忘記標記爲接受關閉。 – 2013-03-06 18:38:51

相關問題