<html>
<head>
<title>纯JS - 面向对象版</title>
<meta http-equiv= "Content-Type"
content= "text/html; charset=utf-8" ></head>
<style type= "text/css" >
#btn, #btn2 { width: 500px; height: 30px; margin:0;padding: 0; list-style: none;}
#btn li, #btn2 li { cursor: pointer; float: left; width: 100px; height: 30px; margin-right: 2px; background: #666; color:#fff; text-align: center; line-height: 30px;}
#btn li.onbtn, #btn2 li.onbtn {background: #f00;}
#tabs div, #tabs2 div {display: none; width: 500px; height: 100px; border: 1px solid #ccc; margin-bottom:10px;}
</style>
<script type= "text/javascript" >
function
tab(btn, tabs) {
var
_this = this ;
var
btn = document.getElementById(btn);
var
tabs = document.getElementById(tabs);
this .btnli = btn.getElementsByTagName( ‘li‘ );
this .tabsdiv = tabs.getElementsByTagName( ‘div‘ );
for
( var
a=0; a< this .btnli.length; a++) {
this .btnli[a].index = a;
this .btnli[a].onmouseover = function () {
_this.onmouseoverfn( this );
}
}
this .tabsdiv[0].style.display = "block" ;
this .btnli[0].className = "onbtn" ;
}
tab.prototype.onmouseoverfn = function (obj) {
for
( var
b=0; b< this .btnli.length; b++) {
this .tabsdiv[b].style.display = "none" ;
this .btnli[b].className = "" ;
}
this .tabsdiv[obj.index].style.display = "block" ;
this .btnli[obj.index].className = "onbtn" ;
}
window.onload = function () {
new
tab( ‘btn‘ , ‘tabs‘ );
new
tab( ‘btn2‘ , ‘tabs2‘ );
}
</script>
<body>
<div>
<ul id= "btn" >
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul>
<div id= "tabs" >
<div>选项一内容</div>
<div>选项二内容</div>
<div>选项三内容</div>
</div>
</div>
<div>
<ul id= "btn2" >
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul>
<div id= "tabs2" >
<div>选项一内容</div>
<div>选项二内容</div>
<div>选项三内容</div>
</div>
</div>
</body>
</html>
|