Native JS Implementation Tab effect

Source: Internet
Author: User

<! DOCTYPE html>


<meta charset= "Utf-8" >
<meta http-equiv= "x-ua-compatible" content= "ie=edge,chrome=1" >
<meta name= "viewport" content= "width=device-width; initial-scale=1.0; minimum-scale=1.0; maximum-scale=2.0 "/>
<title> tab-leaf making </title>
<meta name= "description" content= "JS tab"/>
<meta name= "keywords" content= "leaves"/>
<style type= "Text/css" >
*{padding:0; margin:0; border:none;list-style:none;}
body{width:800px;margin:50px Auto;}
. Tab{width:800px;color: #fff; text-align:center;}
. tab. Tit{overflow:hidden;}
. tab. Tit li{display:inline-block;float:left;margin-right:10px;width:100px;height:30px;line-height:30px; Background:blue;}
. tab. Tit li.on{background:red;}
. Tab>div{display:none;width:800px;height:300px;line-height:300px;background:red;font-size:80px;font-weight: Bold;}
. Tab>div.on{display:block;}
</style>

<body>
<div id= "tab1" class= "tab" >
<ul class= >
<li>111</li>
<li>222</li>
<li>333</li>
</ul>
<div>111</div>
<div>222</div>
<div>333</div>
</div>
<div id= "tab2" class= "tab" >
<ul class= >
<li>111</li>
<li>222</li>
<li>333</li>
</ul>
<div>111</div>
<div>222</div>
<div>333</div>
</div>
<script type= "Text/javascript" >
Function tab (TABID) {
var tab = document.getElementById (tabId);
var olis = tab.getelementsbytagname ("Li");
var odiv = tab.getelementsbytagname ("div");

Olis.item (0). ClassName = "on";//Add class name to Li[0];
Odiv.item (0). ClassName = "on";//Add class name to Div[0];

for (Var i=0;i<olis.length;i++) {
Olis.item (i). Wkxindex = i;//custom attributes;
Olis.item (i). onclick = function () {//click Li;
for (Var i=0;i<olis.length;i++) {//Use the loop to set all the on class names on Li and div to null when you click on Li;
Olis.item (i). ClassName = null;
Odiv.item (i). ClassName = null;
}
Olis.item (this.wkxindex). ClassName = "on";//use a custom attribute to put the given class name of the current click on (add Style);
Odiv.item (this.wkxindex). ClassName = "on";
}
}
}
tab ("TAB1");//pass the ID value of the div using the tab;
tab ("TaB2");
</script>
</body>

Native JS Implementation Tab effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.