|
1 | 1 | // Simple Tab Switching Logic |
2 | | -document.addEventListener('DOMContentLoaded', function () { |
3 | | - const tabContainers = document.querySelectorAll('.code-tabs'); |
| 2 | +function activateTab(clickedButton, container) { |
| 3 | + const buttons = container.querySelectorAll('.tab-header button'); |
| 4 | + const contents = container.querySelectorAll('.tab-content'); |
4 | 5 |
|
5 | | - tabContainers.forEach(container => { |
6 | | - const headers = container.querySelector('.tab-header'); |
7 | | - const buttons = headers.querySelectorAll('button'); |
8 | | - const contents = container.querySelectorAll('.tab-content'); |
| 6 | + // Deactivate all |
| 7 | + buttons.forEach(btn => btn.classList.remove('active')); |
| 8 | + contents.forEach(content => content.classList.remove('active')); |
9 | 9 |
|
10 | | - buttons.forEach(button => { |
11 | | - button.addEventListener('click', () => { |
12 | | - buttons.forEach(btn => btn.classList.remove('active')); |
13 | | - contents.forEach(content => content.classList.remove('active')); |
| 10 | + // Activate clicked button |
| 11 | + clickedButton.classList.add('active'); |
14 | 12 |
|
15 | | - button.classList.add('active'); |
| 13 | + // Activate corresponding content |
| 14 | + const tabId = clickedButton.getAttribute('data-tab'); |
| 15 | + const content = container.querySelector(`.tab-content[data-tab="${tabId}"]`); |
| 16 | + if (content) { |
| 17 | + content.classList.add('active'); |
| 18 | + } |
| 19 | +} |
16 | 20 |
|
17 | | - const tabId = button.getAttribute('data-tab'); |
18 | | - const content = container.querySelector(`.tab-content[data-tab="${tabId}"]`); |
19 | | - if (content) { |
20 | | - content.classList.add('active'); |
21 | | - } |
22 | | - }); |
23 | | - }); |
| 21 | +function initTabContainer(container) { |
| 22 | + const buttons = container.querySelectorAll('.tab-header button'); |
| 23 | + buttons.forEach(button => { |
| 24 | + button.addEventListener('click', () => activateTab(button, container)); |
24 | 25 | }); |
| 26 | +} |
| 27 | + |
| 28 | +document.addEventListener('DOMContentLoaded', () => { |
| 29 | + const tabContainers = document.querySelectorAll('.code-tabs'); |
| 30 | + tabContainers.forEach(initTabContainer); |
25 | 31 | }); |
0 commit comments