جواب «Mark the Active Tab» چالش 4 از جلسهی 14
سؤال
صفحهها عوض میشوند، ولی نوار پایین همیشه دکمهی خانه را روشن نشان میدهد. به آخر show(name) اضافه کن: کلاس active را از همهی nav buttonها بردار و به دکمهای بده که id آن "tab-" + name است.
راهنما
مثل حلقهی صفحهها: document.querySelectorAll("nav button") و برای هر کدام classList.remove("active"). بعد document.getElementById("tab-" + name).classList.add("active").
یک جواب درست
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My App</title>
<style>
body { font-family: sans-serif; margin: 0; background-color: #f6f7fb; }
header { background-color: #3949ab; color: white; padding: 12px 16px; }
.screen { max-width: 420px; margin: 0 auto; padding: 16px 16px 90px; }
nav { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background-color: white; }
nav button { flex: 1; min-height: 56px; font-size: 16px; border: none; background-color: white; }
nav button.active { background-color: #ffd23f; font-weight: bold; }
#list, #settings { display: none; }
</style>
</head>
<body>
<header><h1 id="title">My App</h1></header>
<section class="screen" id="home">
<h2>Home</h2>
<p>Welcome! Pick a screen below.</p>
</section>
<section class="screen" id="list">
<h2>My List</h2>
<ul>
<li>Read a book</li>
<li>Drink water</li>
</ul>
</section>
<section class="screen" id="settings">
<h2>Settings</h2>
<p>Nothing here yet.</p>
</section>
<nav>
<button id="tab-home" class="active" onclick="show('home')">Home</button>
<button id="tab-list" onclick="show('list')">List</button>
<button id="tab-settings" onclick="show('settings')">Settings</button>
</nav>
<script>
function show(name) {
let screens = document.querySelectorAll(".screen");
for (let screen of screens) {
screen.style.display = "none";
}
document.getElementById(name).style.display = "block";
let tabs = document.querySelectorAll("nav button");
for (let tab of tabs) {
tab.classList.remove("active");
}
document.getElementById("tab-" + name).classList.add("active");
}
</script>
</body>
</html>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن