0

プラグインのメソッドにパラメーターを追加する方法を探しています。

この例ではupdateメソッドを使用していますが、何を更新するかを指示するために追加のパラメーターが必要です。

//プラグインラッパー

  ;(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');
4

1 に答える 1

0

何をしようとしているのかわかりませんが、に引数を追加して、または任意のメソッド$.fn[pluginName]に使用できます。update

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

そして、呼び出し部分では、

data[option](mydata);
于 2013-03-06T18:22:07.197 に答える