Example of JS implementation based on object-oriented tabs, js example

Source: Internet
Author: User

Example of JS implementation based on object-oriented tabs, js example

This article describes the implementation of JS tabs based on object-oriented. We will share this with you for your reference. The details are as follows:

Intermediate Transitional Link:Rewrite the process-oriented program into an object-oriented form

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

Notes for rewriting:

1. premise: All code must be included in window. onload.
2. Remove function nesting (the nested function in window. onload goes out of window. onload)
3. functions cannot be nested, but global variables can exist. (For example, after the onclick function is taken out, aBtn is a private variable in the window. onload function, and The onclick function cannot be used)

Process:

1. onload (initialize the entire program) → Constructor (Initialize an object)
2. Global variables → Properties
3. Functions → Methods

Window. onload = function () {var oTab = new TabSwitch ("div1");} function TabSwitch (id) {var oDiv = document. getElementById (id); this. aBtn = oDiv. getElementsByTagName ('input'); this. aDiv = oDiv. getElementsByTagName ('div '); var I = 0; var _ this = this; // this is the new object, that is, oTab for (I = 0; I <this. aBtn. length; I ++) {this. aBtn [I]. index = I; this. aBtn [I]. onclick = function () {_ this. tab (this); // upload the clicked button to the following in the form of parameters }}; TabSwitch. prototype. tab = function (oBtn) {for (I = 0; I <this. aBtn. length; I ++) {this. aBtn [I]. className = ''; this. aDiv [I]. style. display = 'none';} oBtn. className = 'active'; // the button to be clicked, instead of the new object, so this is not required here. aDiv [oBtn. index]. style. display = 'block ';}

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.