我會suggestm這從jQuery的教程插件(但有幾個方法) :
(function($)
{
var globalSettings=
{
width:400,
height:400
};
var methods =
{
init : function(options)
{
return this.each(function()
{
var $this=$(this);
var settings=$.extend({}, globalSettings, options);
//your code
});
},
destroy : function()
{
return this.each(function()
{
});
},
option : function(option, value)
{
if(value!=null && value!=undefined)
{
return this.each(function(){
var $this=$(this);
var curr_setts=$this.data('settings');
curr_setts[option]=value;
$this.data('settings',curr_setts);
});
}
else
{
var $this=$(this);
var curr_setts=$this.data('settings');
return curr_setts[option];
}
}
};
$.fn.siter=function(method, options)
{
if(methods[method])
{
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if(typeof method === 'object' || !method)
{
return methods.init.apply(this, arguments);
}
else
{
$.error('Method ' + method + ' does not exist on jQuery');
}
};
})(jQuery);
您是否希望用戶能夠更改默認選項? – pimvdb 2012-01-14 22:43:26
是的,我確實需要這個。 – Roland 2012-01-14 23:13:56