forked from JuanIrache/eloquent-javascript-exercises
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_3_tabs.html
37 lines (37 loc) · 1.13 KB
/
15_3_tabs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<tab-panel>
<div data-tabname="one">Tab one</div>
<div data-tabname="two">Tab two</div>
<div data-tabname="three">Tab three</div>
</tab-panel>
<script>
function asTabs(node) {
let tabs = Array.from(node.querySelectorAll('div'));
let buttons = [];
let header = document.createElement('div');
node.insertBefore(header, node.firstChild);
for (let i = 0; i < tabs.length; i++) {
let tab = tabs[i];
let button = document.createElement('button');
if (i > 0) {
tab.style.display = 'none';
} else {
button.style.color = 'red';
}
button.textContent = tab.getAttribute('data-tabname');
button.addEventListener('click', () => {
for (let butt of buttons) butt.style.color = 'black';
button.style.color = 'red';
for (let tab of tabs) {
if (tab.getAttribute('data-tabname') === button.textContent) {
tab.style.display = '';
} else {
tab.style.display = 'none';
}
}
});
header.appendChild(button);
buttons.push(button);
}
}
asTabs(document.querySelector('tab-panel'));
</script>