Analyze the code for personal use of native JS to obtain class name elements:
The code copy is as follows:
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;
}
The parent parameter is optional, but you need to first determine whether it exists and is a node dom element parent != undefined&&parent.nodeType==1 , nodeType == 1 can determine whether the node is a dom element. In Firefox browser, blanks can also be regarded as nodes (.childnodes). Use this attribute to determine whether it is a dom element and exclude whitespace symbols.
Remove the class name of the element:
The code copy is as follows:
var cur = new RegExp(this.sCur,'g'); //this.sCur is the class name, which is saved here using variables such as: this.sCur = "cur";
this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,'');
Call example:
The code copy is as follows:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
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: #ffff;}
.box p {display: none;padding: 30px;}
/*tabB*/
#tabB {width: 450px;}
.box .tab-cur02 {background-color: #025023;}
</style>
</head>
<body>
<div>
<strong>Used reading:</strong> <br>
{'tabBtn':'#tabA .tab-i','tabCon':'#tabA .tab-c','cur':'tab-cur'} [Required] <br>
(1) 'tabBtn':'#tabA .tab-i','tabCon':'#tabA .tab-c' selector: only #id .className is supported, (ID + space + class name) [Required]<br>
(2) 'cur':'tab-cur' (default) : Toggle button's current status (class name) [required]<br>
(3) 'type':'mouseover'|| 'clicl' By default, click [optional]
</div>
<h3>tabA</h3>
<pre>new LGY_tab({'tabBtn':'#tabA .tab-i',
'tabCon':'#tabA .tab-c',
'cur':'tab-cur'
});
</pre>
<div id="tabA">
<ul>
<li>btn-A</li>
<li>btn-B</li>
</ul>
<p>con-A</p>
<p>con-B</p>
</div>
<h3>tabB</h3>
<pre>new LGY_tab({'tabBtn':'#tabB .tab-i',
'tabCon':'#tabB .tab-k',
'cur':'tab-cur02',
'type':'mouseover'
});
</pre>
<div id="tabB">
<ul>
<li>btn-A</li>
<li>btn-B</li>
<li>btn-C</li>
</ul>
<p>con-A</p>
<p>con-B</p>
<p>con-C</p>
</div>
<script type="text/javascript" src="Code snippet below.js"></script>
<script type="text/javascript">
//
new LGY_tab({'tabBtn':'#tabA .tab-i',
'tabCon':'#tabA .tab-c',
'cur':'tab-cur'
});
//
new LGY_tab({'tabBtn':'#tabB .tab-i',
'tabCon':'#tabB .tab-k',
'cur':'tab-cur02',
'type':'mouseover'
});
//test
//
new LGY_tab({'tabBtn':'#tabB .tab-j',
'tabCon':'#tabB .tab-k',
'cur':'tab-cur02',
'type':'mouseover'
});
</script>
</body>
</html>
JS detailed code:
The code copy is as follows:
function LGY_tab(option){
this.oTab_btn = this.getDom(option.tabBtn);//Switch the clicked element
this.oTab_clist = this.getDom(option.tabCon); //Switched content
if(!this.oTab_btn || !this.oTab_clist) return;
this.sCur = option.cur; //Activated state
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 = 'none';
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 = 'block';
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 = 'block';
}
}
}
}
Final rendering display:
Is the effect great? It also has good compatibility and the code is simple, which can completely replace the huge jQuery tab switching plug-in.