(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插件的通用模版
...........................................................
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.