This article mainly introduces the instance code of the JavaScripttab tab. For more information, see the following, rewrite an existing Tab switch function to the form of a javascript plug-in.
Native function writing
The simplest way to rewrite a javascript method as a js plug-in is to mount this method to a window global object.
Let's take a look at the original code written using the function:
Tab.html
Jquery_hjb_tab plug-in demo
Content 1
Content 2
Content 3
Content 4
Script window. onload = h_tab ('Tab '); function h_tab (tabId) {var oLinks = document. getElementById (tabId ). getElementsByTagName ("a"); var oCons = document. getElementById (tabId ). getElementsByTagName ("section"); for (var I = 0; I
H.css
@ Charset "UTF-8"; // * // author: hjb2722404 // description: // date: 2016/2/18 */. tabs ul {width: 100%; list-style-type: none ;}. tabs ul li {width: 48%; display: inline-block; margin: 0; padding: 0 ;}. tabs ul li a {border-bottom: 3px solid # cccccc; width: 100%; height: 100%; display: block; text-align: center; min-height: 40px; line-height: 40px; text-decoration: none; font-family: ""; color: # a94442 }. tabs ul li. cur {border-bottom: 3px solid # f26123 ;}. tabCons section {display: none ;}. tabCons section: nth-child (1) {display: block ;}
The above two pieces of code are the basic code, and then we will improve the code step by step.
Native plug-in writing
Now, we will rewrite this method to the plug-in mounted to the window object:
Tab.html
...... // The following is the first change