(function($){
//单例类
function MaxLength(){
this._default={
name:'',
id:''
};
this._markerClassName="";
this._propertyName="";
}
MaxLength.prototype={
//设置默认参数
setDefaults:function(options){
$.extend(this._default,options);
},
//初始化
_attachPlugin:function(target,options){
target=$(target);
options= $.extend({},this._default,options);
if(target.hasClass(this._markerClassName)){//防止多次初始化
return false;
}
target.addClass(this._markerClassName);
target.data(this._propertyName,options);//保存配置到特定的元素
},
//设置,获取选项
_optionPlugin:function(target,options,value){
target=$(target);
var inst=target.data(this._propertyName);
if(options&&typeof options=="string"&&!value){//如果是获取配置
return inst[options];
}else if(options&&value){//如果是设置一个配置
var name=options;
options={};
options[name]=value;
}
$.extend(inst,options);
},
//移除插件
_destroyPlugin:function(target){
target=$(target);
if(!target.hasClass(this._markerClassName)){
return;
}
//移除元素的标记类
//移除附加在元素上的data数据
//移除附加在元素上的事件
//移除初始化时创建的元素等
}
};
$.extend($.fn,{
log:function(options){
var otherArgs=Array.prototype.slice.call(arguments,1);//提取第二个参数
var getters=["option"];
if(typeof options=='string'&& $.inArray(options,getters)>-1){//如果是调用返回值方法
return plugin["_"+options+"Plugin"].apply(plugin,[this[0]].concat(otherArgs))
;
}
return this.each(function(){
if(typeof options=='string'){//如果是调用插件方法
plugin['_'+options+'Plugin'].apply(plugin,[this].concat(otherArgs));
}else{//如果是初始化插件
plugin._attachPlugin(this,options||{});
}
})
}
}) ;
//单例实例
var plugin= $.log=new MaxLength();
}(jQuery));
jquery插件的通用模版
...........................................................
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.