HTML Document is here:-
<!DOCTYPE html>
<html>
<head>
<title>Tabs</title>
<link rel=alternate href="hello.html" hreflang=en type=text/html title="English HTML">
<link type="text/css" href="tabs.css" rel="stylesheet" />
<meta charset="UTF-8"/>
</head>
<body style="margin: 0;">
<center>
<script src="tabs.js"></script>
<ul id='tabs'>
<li><a href="#one">Tab 1</a></li>
<li><a href="#two">Tab 2</a></li>
<li><a href="#three">Tab 3</a></li>
<li><a href="#four">Tab 4</a></li>
<li><a href="#five">Tab 5</a></li>
</ul>
<div id='one' class="tabContent">
<p>Tab One</p>
</div>
<div id='two' class="tabContent">
<p>Tab Two</p>
</div>
<div id='three' class="tabContent">
<p>Tab Three</p>
</div>
<div id='four' class="tabContent">
<p>Tab Four</p>
</div>
<div id='five' class="tabContent">
<p>Tab Five</p>
</div>
</center>
</body>
</html>
Here is the JavaScript
var tabLinks=new Array();
var contentDivs=new Array();
onload=function init(){
var tabListItems=document.getElementById('tabs').childNodes;
for(var i=0; i<tabListItems.length; i++){
if(tabListItems[i].nodeName=="LI"){
var tabLink=getFirstChildWithTagName(tabListItems[i], "A");
var id=getHash(tabLink.getAttribute('href'));
tabLinks[id]=tabLink;
contentDivs[id]=document.getElementById(id);
}
}
var i=0;
for(var id in tabLinks){
tabLinks[id].onclick=showTab;
tabLinks[id].onfocus=function(){
this.blur();
};
if(i==0)
tabLinks[id].className='selected';
i++;
}
var i=0;
for(var id in contentDivs){
if(i!=0)
contentDivs[id].className="tabContent hide";
i++;
}
}
function showTab(){
var selectedId=getHash(this.getAttribute('href'));
for(var id in contentDivs){
if(id==selectedId){
tabLinks[id].className='selected';
contentDivs[id].className='tabContent';
} else{
tabLinks[id].className='';
contentDivs[id].className='tabContent hide';
}
}
return false;
}
function getFirstChildWithTagName(element, tagName){
for(var i=0; i<element.childNodes.length; i++){
if(element.childNodes[i].nodeName==tagName)
return element.childNodes[i];
}
return null;
}
function getHash(url){
var hashPos=url.lastIndexOf('#');
return url.substring(hashPos+1);
}
And Here is the CSS
ul#tabs{
font-family: Lucida Sans Unicode, Lucida Grande, sans-serif;
font-size: 1.2em;
list-style-type: none;
margin: 0 0 0 0;
padding: 0;
width: 750px;
}
ul#tabs li{
float: left;
padding: 0;
margin: 0 2px 0 0;
}
ul#tabs li a{
color: white;
margin: 0;
padding: 5px 20px;
background-color: #3b5998;
border-top: 6px #3b5998 solid;
font-weight: bolder;
display: block;
}
ul#tabs li a:hover{
color: white;
background-color: #294a8f;
border-top: 6px #294a8f solid;
text-decoration: none;
}
ul#tabs li a.selected{
color: white;
background-color: #294a8f;
border-top: #294a8f solid;
border-bottom: 3px #294a8f solid;
text-decoration: none;
}
div.tabContent{
width: 710px;
clear: both;
border: 10px #294a8f solid;
padding: 10px;
margin: 0;
}
div.tabContent.hide{
display: none;
}
Comments