بتل کد Battle Codeدارد باز می‌شود…

جواب «Colour Themes» چالش 8 از جلسه‌ی 15

سؤال

دفترچه‌ی خاطرات سه رنگ دارد. CSS آماده است: کلاس‌های theme-blue، theme-green و theme-pink روی body متغیر --main را عوض می‌کنند. تابع setTheme(name) را بنویس: هر سه کلاس را از body بردارد، "theme-" + name را بگذارد و name را با salam.save("theme", name) ذخیره کند. آخر اسکریپت setTheme(salam.load("theme", "blue")) را صدا بزن.

راهنما

classList.remove چند کلاس را با هم می‌گیرد: document.body.classList.remove("theme-blue", "theme-green", "theme-pink");.

یک جواب درست

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Diary</title>
<style>
body { font-family: sans-serif; margin: 0; }
body.theme-blue { --main: #1e88e5; }
body.theme-green { --main: #2e7d32; }
body.theme-pink { --main: #d81b60; }
header { background-color: var(--main); color: white; padding: 12px 16px; }
.app { max-width: 420px; margin: 0 auto; padding: 16px; }
button { min-height: 44px; font-size: 16px; }
</style>
</head>
<body>
<header><h1>My Diary</h1></header>
<div class="app">
  <p>Pick a colour for your app:</p>
  <button id="blue" onclick="setTheme('blue')">Blue</button>
  <button id="green" onclick="setTheme('green')">Green</button>
  <button id="pink" onclick="setTheme('pink')">Pink</button>
</div>

<script>
function setTheme(name) {
  document.body.classList.remove("theme-blue", "theme-green", "theme-pink");
  document.body.classList.add("theme-" + name);
  salam.save("theme", name);
}

setTheme(salam.load("theme", "blue"));
</script>
</body>
</html>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن