Performance: Tab navigation via JavaScript
This affects interaction responsiveness and visual stability when switching tabs on a page.
const tabs = document.querySelectorAll('.tab'); tabs.forEach(tab => { tab.addEventListener('click', () => { const activeContent = document.querySelector('.tab-content.active'); if (activeContent) activeContent.classList.remove('active'); const target = document.querySelector(tab.dataset.target); target.classList.add('active'); }); }); /* CSS: .tab-content { display: none; } .tab-content.active { display: block; } */
document.querySelectorAll('.tab').forEach(tab => { tab.addEventListener('click', () => { document.querySelectorAll('.tab-content').forEach(content => { content.style.display = 'none'; }); const target = document.querySelector(tab.dataset.target); target.style.display = 'block'; }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct style changes on each tab content | Multiple style changes on many elements | N+1 reflows per tab switch | High paint cost due to layout thrashing | [X] Bad |
| Toggle CSS classes for visibility | Single class change on active elements | 1 reflow per tab switch | Lower paint cost with batched updates | [OK] Good |
new bootstrap.Tab(element).show() do in Bootstrap tab navigation?new bootstrap.Tab(element).show() is designed to activate a specific tab.show() displays the content linked to the tab and marks the tab as active.new bootstrap.Tab(element)..show() to activate the tab.<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-bs-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="profile-tab" data-bs-toggle="tab" href="#profile" role="tab">Profile</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="home" role="tabpanel">Home Content</div>
<div class="tab-pane fade" id="profile" role="tabpanel">Profile Content</div>
</div>
<script>
var profileTab = document.querySelector('#profile-tab');
var tab = new bootstrap.Tab(profileTab);
tab.show();
</script>profile-tab and calls show() on it.show() activates the Profile tab and displays its content, hiding the Home content.var tabTrigger = document.getElementById('settings-tab');
tabTrigger.show();show() method.new bootstrap.Tab(tabTrigger) before calling show().<button id="activate-settings">Go to Settings</button>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-bs-toggle="tab" href="#home" role="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="settings-tab" data-bs-toggle="tab" href="#settings" role="tab">Settings</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="home" role="tabpanel">Home Content</div>
<div class="tab-pane fade" id="settings" role="tabpanel">Settings Content</div>
</div>
<script>
// Which code activates the Settings tab on button click?
</script>addEventListener('click', ...) is a standard way to handle button clicks.bootstrap.Tab instance for the 'settings-tab' element and calling show() activates the tab correctly.Pass the test and this pattern moves unknown