1 | function makeTabSet(parentElement) { |
---|
2 | var tabList = document.createElement("ul"); |
---|
3 | tabList.className = "tabs"; |
---|
4 | var contentDivs = document.createElement("div"); |
---|
5 | |
---|
6 | function makeTab(div) { |
---|
7 | var title = div.firstChild; |
---|
8 | while (title.nodeType != 1) title = title.nextSibling; |
---|
9 | var tabItem = document.createElement("li"); |
---|
10 | if (!tabList.childNodes.length) tabItem.className = "active"; |
---|
11 | var link = document.createElement("a"); |
---|
12 | link.href = "#"; |
---|
13 | link.appendChild(title.firstChild); |
---|
14 | tabItem.appendChild(link); |
---|
15 | |
---|
16 | var contentDiv = document.createElement("div"); |
---|
17 | contentDiv.className = "tab-content"; |
---|
18 | while (div.childNodes.length) contentDiv.appendChild(div.firstChild); |
---|
19 | if (tabList.childNodes.length) contentDiv.style.display = "none"; |
---|
20 | |
---|
21 | link.onclick = function() { |
---|
22 | var child = contentDivs.firstChild; |
---|
23 | while (child) { |
---|
24 | if (child != contentDiv && child.nodeType == 1) { |
---|
25 | child.style.display = "none"; |
---|
26 | } |
---|
27 | child = child.nextSibling; |
---|
28 | } |
---|
29 | var item = tabList.firstChild; |
---|
30 | while (item) { |
---|
31 | if (item.nodeType == 1) { |
---|
32 | item.className = item != tabItem ? "" : "active"; |
---|
33 | } |
---|
34 | item = item.nextSibling; |
---|
35 | } |
---|
36 | contentDiv.style.display = "block"; |
---|
37 | return false; |
---|
38 | } |
---|
39 | contentDivs.appendChild(contentDiv); |
---|
40 | tabList.appendChild(tabItem); |
---|
41 | } |
---|
42 | |
---|
43 | var divs = parentElement.getElementsByTagName("div"); |
---|
44 | for (var i = 0; i < divs.length; i++) { |
---|
45 | var div = divs[i]; |
---|
46 | if (!/\btab\b/.test(div.className)) { |
---|
47 | continue; |
---|
48 | } |
---|
49 | makeTab(div); |
---|
50 | } |
---|
51 | |
---|
52 | parentElement.appendChild(tabList); |
---|
53 | parentElement.appendChild(contentDivs); |
---|
54 | } |
---|