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 ';}