怎么封装自己的jquery插件2

时间:2022-03-29 20:35:40
(function($){
var Tab=function(el){
var _this=this;
this.el=el;
//配置默认参数
this.config={}
//如果参数存在就扩展默认的参数
if(this.getConfig()){
$.extend(this.config,this.getConfig())
}
};
Tab.prototype={
//添加方法
xxx:function(){},
//获取配置的参数
getConfig:function(){
//拿一下dom的data-config
var config = this.el.attr('data-config');
//确保有配置的参数
if(config&&config!=''){
return $.parseJSON(config)
}else{
return null
}
}
};
//封装为jq方法
$.fn.extend({
tab:function(){
//this指代调用的元素集合
this.each(function(){
//this指代每一个元素
new Tab($(this))
})
return this;//链式调用
}
});
window.Tab=Tab;
}(jQuery));
调用:$(div).tab()