JavaScripttab plug-in instance code _ javascript skills

Source: Internet
Author: User
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
 

  • Tab1
  • Tab2
  • Tab3
  • Tab4

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

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.