Common and simple table tab implementation in JavaScript (2)

Source: Internet
Author: User

Return to original. After the style switch, return the control to the page, that is, table. js only controls the switching style and record operations: CopyCode The Code is as follows: <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> New web project </title>
<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;
}
. Content {
Height: 600px;
Width: 400px;
Border-Right: 1px solid # CCC;
Margin-left: 140px;
Padding: 20px;
Display: none;
}
</Style>
</Head>
<Body>
<Div class = "sidebar" id = "sidebar">
<Ul>
<Li point = "Table1">
Option 1
</LI>
<Li point = "Table2">
Option 2
</LI>
<Li point = "table3">
Option 3
</LI>
<Li point = "table4">
Option 4
</LI>
<Li point = "table5">
Option 5
</LI>
</Ul>
</Div>
<Div id = "Table1" class = "content">
This is the content of the first tab.
</Div>
<Div id = "Table2" class = "content">
This is the content of the second tab.
</Div>
<Div id = "table3" class = "content">
This is the content of the third tab.
</Div>
<Div id = "table4" class = "content">
This is the content of the fourth tab.
</Div>
<Div id = "table5" class = "content">
This is the content of the fifth tab.
</Div>
</Body>
<SCRIPT type = "text/JavaScript" src = "table. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
// Available callback function parameters: obj. lastindex (last option index) obj. Index (current option index) obj. Arr (Tab element array)
VaR back = function (OBJ)
{
VaR lastpoint = obj. Arr [obj. lastindex]. getattribute ("point ");
VaR curentpoint = obj. Arr [obj. Index]. getattribute ("point ");
Document. getelementbyid (lastpoint). style. Display = "NONE ";
Document. getelementbyid (curentpoint). style. Display = "Block ";
}
// The parameters are as follows: The selected style callback function of the Option block ID is selected by default (starts from 0)
Table ("sidebar", "active", back, 0 );
</SCRIPT>
</Html>

Copy codeThe Code is as follows: // available callback function parameters: obj. lastindex (last option index) obj. Index (current option index) obj. Arr (Tab element array)
VaR back = function (OBJ)
{
VaR lastpoint = obj. Arr [obj. lastindex]. getattribute ("point ");
VaR curentpoint = obj. Arr [obj. Index]. getattribute ("point ");
Document. getelementbyid (lastpoint). style. Display = "NONE ";
Document. getelementbyid (curentpoint). style. Display = "Block ";
}
// The parameters are as follows: The selected style callback function of the Option block ID is selected by default (starts from 0)
Table ("sidebar", "active", back, 0 );

The table. js code is as follows: Copy code The Code is as follows :/**
* @ Author sky
*/
VaR table = function (ID, active, callback, index)
{
Table [ID] = new table (ID, active, callback, index );
Table [ID]. BIND ();
}
VaR table = function (ID, active, callback, index)
{
This. Index = parseint (INDEX) | 0; // current index
This. lastindex = This. Index; // last index
This. Callback = callback | function (){};
This. Active = active | "active ";
This. ID = ID;
This. Arr = Document. getelementbyid (ID). getelementsbytagname ("Li ");
}
Table. Prototype = {
BIND: function ()
{
// Initialize the option style
This. settable (this. Index );
// Bind events
VaR _ Self = this;
For (VAR I = 0; I <this. Arr. length; I ++)
{
This. Arr [I]. setattribute ("extatt", I); // hook
This. Arr [I]. onclick = function (E)
{
VaR _ e = Window. Event | E;
VaR _ target = _ E. srcelement | _e.tar get;
_ Self. settable (parseint (_ target. getattribute ("extatt ")));
}
}
},
Settable: function (INDEX)
{
This. lastindex = This. index;
This. Index = index;
// Clear the selected Style
This. Arr [This. lastindex]. classname = "";
// Activate the style of the current option
This. Arr [This. Index]. classname = This. Active;
// Execute the callback function
This. Callback (Table [This. ID]);
}
}

Related Article

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.