Common tab Based on JavaScript (strong versatility), cripttab

Source: Internet
Author: User

Common tab Based on JavaScript (strong versatility), cripttab

Tabs are applied on a large number of websites. Although they have different forms, they all achieve the same purpose. They are generally used for classification or space saving, the following is a code example of a tab, which is highly versatile. Let's share it with you.

The code example is as follows:

<! DOCTYPE html> 

The code above implements the tab function. The following describes the implementation process.

I. Implementation principle:

It seems that there are a lot of code. In fact, the principle is very simple. Here we will only briefly introduce the principle. For details, refer to the code comments and rely on your own ideas. The title of the default status tab is displayed, and the first title is assigned to the specified style class. Only the first tab content is displayed, and others are hidden, this achieves the first selected effect by default. Each tab title will be registered with the specified event handler function. When you click or span an operation, you can implement the corresponding switchover, mainly through traversal. This is not described here, see Code comments.

Ii. Code comments:

1. var EventUtil = {} declares a direct volume of objects. It internally obtains the event object, binds the event source object to the event processing function, and is compatible with various browsers.
2. getEvent: function (event) {return event? Event: window. event;}, get event object, compatible with various browsers.
3. getTarget: function (event) {return event.tar get | event. srcElement ;}, get the event source object, and be compatible with various browsers.
4. addHandler: function (element, type, handler) {}, registering event processing functions is compatible with various browsers.
5. function tabSwitch (inClassName, triggerType) {}. This function can be used to specify an event handler for the specified object registration. It has two parameters. The first parameter is the style class name, used to obtain objects with such names, and the second is the event type.
6. if (document. querySelectorAll) is used to determine whether the browser supports the querySelectorAll function.
7. var tabs = document. querySelectorAll ("." + inClassName). If yes, obtain the object with the specified class name.
8. var divs = document. getElementsByTagName ("div") to obtain the div object set.
9. var tabs = new Array (), creates an Array, and terms store div objects with the specified style class.
10. for (var k = 0, lenDiv = divs. length; k <lenDiv; k ++), traverse the obtained div object set.
11. if (divs [k]. className. indexOf (inClassName)>-1), if the Style Class Name of the div contains the specified style class name.
12. tabs. push (divs [k]) to store the div into an array.
13. for (var j = 0, len = tabs. length; j <len; j ++), which traverses the array containing div. In this example, the div whose class attribute value is tab1 and tab2.
14. var tab = tabs [j]. assign a reference to a div object to the tab.
15. (function () {}) () declares an anonymous function and runs it.
16. var nameUl = tab. getElementsByTagName ("ul") [0] to obtain the first entry in the ul set, that is, the title of the tab.
17. var content = tab. getElementsByTagName ("ul") [1]. Obtain the content of the tab.
18. nameUl. getElementsByTagName ("li") [0]. className = "selected", set the style class value of the first title of the tab title to selected.
19. content. getElementsByTagName ("li") [0]. style. display = "block", set the first part of the tab content to display.
20. eventUtil. addHandler (nameUl, triggerType, function (event) {}). This function is the core part of the Implementation tab and has three parameters. The first parameter is the ul object of the title part, the second is the event type, and the third function is the event processing function to be registered.
21. var event = EventUtil. getEvent (event) to get the event object.
22. var target = EventUtil. getTarget (event) to obtain the event source object.
23. if (target. nodeName. toLowerCase () = "li"), determine whether the label name of the event source object is li.
24. var nameList = nameUl. getElementsByTagName ("li"). Obtain the li element set in the tab TITLE section.
25. var contentList = content. getElementsByTagName ("li"), which is used to obtain the li elements in the content of the tab.
26. for (var I = 0, len = nameList. length; I <len; I ++), traverse the li element set.
27. nameList. className = "", clears the style class of each title li element.
28. contentList. style. display = "none", which hides the li of the content of each tab.
29. if (nameList = target), if the index title li is the event source object, that is, the li clicked by the mouse or the li slide by the mouse.
30. nameList. className = "selected", add the specified style class for it.
31. contentList. style. display = "block", which displays the content of the corresponding index li.

The above content is relatively detailed, with code and comments. I hope you can learn about the js implementation tab.

Articles you may be interested in:
  • Javascript achieves the display, structure, and behavior separation tab effect!
  • Yahoo China tab effect code for javascript gradient display
  • A good Web tab implemented with javascript
  • Javascript horizontal and vertical standard tab effect code
  • Code for implementing the tab function in js
  • JavaScript browser tab Effect
  • Code for the tab effect created by php (no js is required)
  • Cross-browser common and reusable tab switch js Code
  • Examples of implementing the special effect of tabs using js
  • Switch the tabs tab using javascript (self-writing native js)
  • Example of hiding when jsp js mouse moves to a specified area to display the tab to exit
  • Js/jQuery simple implementation of the tab Function

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.