The first step: quoting Table.js
Copy Code code as follows:
<script type= "Text/javascript" src= "Table.js" > </script>
Step Two: Define the selected style, such as "active", the ID of the block to which the tab is applied, such as "sidebar", the serial number that is selected by default, such as the first "0";
Step Three: Call the function:
Copy Code code as follows:
<script type= "Text/javascript" >
The parameters are: Default selection Application Block ID selected style
Table (0, "sidebar", "active")
</script>
All OK, the tab response click event, concurrently IE and FF, and so have time to optimize, the effect is as follows:
The HTML source code is as follows:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>new Web project</title>
<script type= "Text/javascript" src= "Table.js" > </script>
<style type= "Text/css" >
. sidebar {
width:140px;
Background: #C9E4D6;
min-height:600px;
Float:left;
Border-left:solid 1px #C8C8C8;
}
. Sidebar ul {
List-style:none;
Text-align:left;
padding:20px 0px 0px 0px;
}
. Sidebar ul li {
border-bottom:1px dotted #C8C8C8;
font-size:14px;
height:30px;
line-height:30px;
padding-left:15px;
margin-left:15px;
Cursor:pointer;
}
. Sidebar. Active {
Background: #fff;
}
</style>
<body>
<div class= "sidebar" id= "sidebar" >
<ul>
<li>
Option One
</li>
<li>
Option two
</li>
<li>
Option three
</li>
<li>
Option Four
</li>
<li>
Option Five
</li>
</ul>
</div>
</body>
<script type= "Text/javascript" >
The parameters are: Default selection Application Block ID selected style
Table (0, "sidebar", "active")
</script>
Table.js
Copy Code code as follows:
/**
* @author Sky
*/
var table=function (index,id,active)
{
Table=new Table (index,id,active);
Table.bind ();
}
var table=function (index,id,active)
{
This.index=parseint (index);
This.arr=document.getelementbyid (ID). getElementsByTagName ("Li");
this.active=active;
}
table.prototype={
Bind:function ()
{
this.arr[this.index].classname=this.active;//initialization
var _self=this;
for (var i = 0; i < this.arr.length; i++)
{
This.arr[i].setattribute ("ext", i);
This.arr[i].onclick = function (e)
{
var _e = window.event| | E
var _target=_e.srcelement | | _e.target;
_self.setclass (parseint (_target.getattribute ("ext"));
}
}
},
Setclass:function (Index)
{
This.arr[this.index].classname= "";
this.arr[index].classname=this.active;
This.index=index;
}
}
Demo download