带主题设置的博客页面
本课属于 Coddy HTML 学习旅程的“JavaScript 实战”单元。第 27 课,共 27 课。
挑战
简单在这个挑战中,我们有一个名为 Lens & Light 的摄影博客页面。页面包含摄影技巧相关的博客文章、深色/浅色主题切换按钮,以及一个简单的新闻简报订阅表单,提交后会显示一条提示消息。
这个摄影博客页面的 HTML 和 CSS 已经准备好了。你的任务是添加 JavaScript 功能:
- 主题切换:为切换按钮添加事件监听器。点击按钮时,切换
dark-mode类在<body>上的状态,从而更改主题。 - 提示消息:提交表单时,通过从提示元素中移除 "hidden" 类,并向提示元素添加 "show" 类,让提示消息显示出来。
亲手试试
<!DOCTYPE html>
<html>
<head>
<title>Photographer's Blog</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Lens & Light</h1>
<button id="themeToggle">Toggle Theme</button>
</header>
<main>
<article class="post">
<h2>Capturing Golden Hour</h2>
<p>
The hour before sunset and after sunrise brings warm, soft light that adds
depth and atmosphere to portraits and landscapes. Every photographer should
experiment with golden hour lighting.
</p>
</article>
<article class="post">
<h2>5 Tips for Street Photography</h2>
<p>
Look for interesting shadows, capture candid emotions, blend into the crowd,
use a prime lens, and always be ready to click. Street photography is about
telling raw and authentic stories.
</p>
</article>
<section class="newsletter">
<h3>Subscribe to Photo Notes</h3>
<form id="newsletterForm">
<input type="email" id="email" placeholder="Enter your email">
<button type="submit">Sign Up</button>
</form>
</section>
</main>
<div id="toast" class="toast hidden">Subscribed successfully! 📸</div>
<script src="script.js"></script>
</body>
</html>
JavaScript 实战中的所有课
自己练习: Web 在线编辑器