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');
是這就是它,非常感謝! – user759235 2013-03-06 18:23:36
@ user759235如果回答了這個問題,請不要忘記標記爲接受關閉。 – 2013-03-06 18:38:51