web前端|js教程
javascript,Tab,选项卡,切换
web前端-js教程
分析个人用原生JS获取类名元素的代码:
安卓网络游戏源码,vscode格式化插件,ubuntu set命令,tomcat对硬件要求,股票智能爬虫,php搭建图片服务器,seo编程入门霸屏推广,建筑工程公司网站模板下载,帝国cms标签模板怎么调用栏目描述lzw
getByClassName:function(className,parent){
var elem = [],
node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName(*):document.getElementsByTagName(*),
p = new RegExp("(^|\\s)"+className+"(\\s|$)");
for(var n=0,i=node.length;n<i;n++){
if(p.test(node[n].className)){
elem.push(node[n]);
}
}
return elem;
}
淘宝刷钻平台源码,ubuntu系统硬盘大小,网络爬虫如何构建,php bat调用php,安顺seo外包lzw
parent参数是可选的,但需要先判断它是否存在,且是节点dom元素 parent != undefined&&parent.nodeType==1 ,nodeType == 1可以判断节点是否为dom元素,在火狐浏览器里面,空白也算是节点(.childnodes),用这个属性就判断是否为dom元素,排除空白符.
大唐豪侠源码,vscode引入html,ubuntu . .sh,tomcat虚假路径,sqlite表查询行数据,原图插件新浪微博,什么前端框架开发TV,家里有小黑爬虫怎么消灭,php 上传图片压缩,沧州seo整站优化,酷纬企业网站管理系统,无限制网页访问软件,软件升级文档模板lzw
移除元素的类名:
var cur = new RegExp(this.sCur,g); //this.sCur就是类名,这里是用变量保存 如:this.sCur = "cur";
this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,\);
调用例子:
body,p,ul,li {padding: 0;margin: 0;}
ul {list-style: none;}
h3 {padding: 5px;background-color: #999;margin-bottom: 10px;}
pre {border: 1px dotted #000;}
.explan {padding: 10px;color: #333;line-height: 1.6;}
.box {width: 300px;height:100px;border: 1px solid #ccc;}
.box ul{height: 30px;line-height: 30px;}
.box ul li {float: left;display: inline;width: 150px;text-align: center;background-color: #eee;cursor: pointer;}
.box .tab-cur {background-color: #000;color: #fff;}
.box p {display: none;padding: 30px;}
/*tabB*/
#tabB {width: 450px;}
.box .tab-cur02 {background-color: #025023;}
使用阅读 :
{ abBtn:#tabA .tab-i, abCon:#tabA .tab-c,cur: ab-cur} 【必选】
(1) abBtn:#tabA .tab-i, abCon:#tabA .tab-c选择器:只支持 #id.className,(ID + 空格 + 类名) 【必选】
(2)cur: ab-cur(默认) :为切换按钮当前状态(类名)【必选】
(3) ype:mouseover|| clicl 默认是点击 【可选】
tabA
new LGY_tab({ abBtn:#tabA .tab-i,
abCon:#tabA .tab-c,
cur: ab-cur
});
btn-A
btn-B
con-A
con-B
tabB
new LGY_tab({ abBtn:#tabB .tab-i,
abCon:#tabB .tab-k,
cur: ab-cur02,
ype:mouseover
});
btn-A
btn-B
btn-C
con-A
con-B
con-C
//
new LGY_tab({ abBtn:#tabA .tab-i,
abCon:#tabA .tab-c,
cur: ab-cur
});
//
new LGY_tab({ abBtn:#tabB .tab-i,
abCon:#tabB .tab-k,
cur: ab-cur02,
ype:mouseover
});
//test
//
new LGY_tab({ abBtn:#tabB .tab-j,
abCon:#tabB .tab-k,
cur: ab-cur02,
ype:mouseover
});
JS详细代码:
function LGY_tab(option){
this.oTab_btn = this.getDom(option.tabBtn);//切换点击的元素
this.oTab_clist = this.getDom(option.tabCon); //切换的内容
if(!this.oTab_btn || !this.oTab_clist) return;
this.sCur = option.cur; //激活的状态
this.type = option.type || click;
this.nLen = this.oTab_btn.length;
this.int();
}
LGY_tab.prototype = {
getId:function(id){
return document.getElementById(id);
},
getByClassName:function(className,parent){
var elem = [],
node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName(*):document.getElementsByTagName(*),
p = new RegExp("(^|\\s)"+className+"(\\s|$)");
for(var n=0,i=node.length;n<i;n++){
if(p.test(node[n].className)){
elem.push(node[n]);
}
}
return elem;
},
getDom:function(s){
var nodeName = s.split( ),
p = this.getId(nodeName[0].slice(1)),
c = this.getByClassName(nodeName[1].slice(1),p);
if(!p || c.length==0) return null;
return c;
},
change:function(){
var cur = new RegExp(this.sCur,g);
for(var n=0;n<this.nLen;n++){
this.oTab_clist[n].style.display = one;
this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,\);
}
},
int:function(){
var that = this;
this.oTab_btn[0].className += +this.sCur;
this.oTab_clist[0].style.display = lock;
for(var n=0;n<this.nLen;n++){
this.oTab_btn[n].index = n;
this.oTab_btn[n][on+this.type] = function(){
that.change();
that.oTab_btn[this.index].className += + that.sCur;
that.oTab_clist[this.index].style.display = lock;
}
}
}
}
最终效果图展示:
效果是不是很棒呢,而且兼容性也不错,代码也很简洁,完全可以替代庞大的jQuery选项卡切换插件了。