【发布时间】:2022-02-11 04:46:26
【问题描述】:
我真的不懂 JavaScript。我的目标是将图标添加到 ????白色的 / ????深色按钮。如果你知道javascript,请帮助我。 我想要什么:
- 如果你按下按钮,它是白色 = 月亮。
- 如果您按下按钮,天黑 = 太阳。
我的字体很棒:
<i class="fa-solid fa-sun"></i><i class="fa-solid fa-moon"></i>
CDN 字体真棒:
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
if (!localStorage.theme) localStorage.theme = "light"
document.body.className = localStorage.theme
toggleThemeBtn.innerText = document.body.classList.contains("dark") ? "white" : "dark"
toggleThemeBtn.onclick = () => {
document.body.classList.toggle("dark")
toggleThemeBtn.innerText = document.body.classList.contains("dark") ? "white" : "dark"
localStorage.theme = document.body.className || "light"
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
min-height: calc(100vh - 100px);
margin-top: 100px;
}
body.dark {
background: #303133;
color: #fff;
}
body.dark .content {
box-shadow: 0 0 5px #1d2225;
}
body.dark button {
box-shadow: 0 0 5px #1d2225;
color: #fff;
}
body.dark button:hover {
box-shadow: 0 0 10px #1d2225;
}
body.dark button:active {
box-shadow: inset 0 0 5px #1d2225;
}
.content {
width: 55%;
margin: auto;
box-shadow: 0 0 3px silver;
padding: 20px;
height: calc(100vh - 100px - 40px);
}
section:not(:first-child) {
margin-top: 30px;
}
.desc {
margin: 10px 0;
opacity: .7;
}
button {
padding: 10px 20px;
border: none;
background: transparent;
box-shadow: 0 0 5px silver;
cursor: pointer;
outline: none;
}
button:hover {
box-shadow: 0 0 10px silver;
}
button:active {
box-shadow: inset 0 0 5px silver;
}
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<title></title>
</head>
<body>
<main class="content">
<i class="fa-solid fa-sun nav-li-icon-bottom" id="moodBW"></i> <i class="fa-solid fa-moon nav-li-icon-bottom"></i>
<section>
<h1>BLock</h1>
<p class="desc">Lorem ipsum dolor sit amet consectetur adipisicing elit. Officiis quod porro voluptate minima quasi a mollitia dicta corrupti nesciunt! Corrupti, quis at? Perspiciatis voluptatibus provident laboriosam vitae voluptatem autem, cupiditate esse nihil est! Ducimus non, corrupti totam quisquam quod et!</p><button id="toggleThemeBtn">Сменить тему на тёмную</button>
</section>
</main>
<script src="script.js">
</script>
</body>
</html>
我不知道为什么,但是我的 HTML / CSS / js sn-p 在 StackOverflow 上不起作用,特别是为此我在 CodePen 上创建了一个页面
codepen url
或https://codepen.io/SergiuC121/pen/dyZWjOd
我不知道这是否有意义,但在我以以下方式更改我的代码并且它实际上工作但它显示真假之后,我删除了白色/深色文本。
var i = document.createElement("i");
var t = document.createElement("i");
t.className = "fa-solid fa-moon nav-li-icon-bottom";
i.className = "fa-solid fa-sun nav-li-icon-bottom";
if (!localStorage.theme) localStorage.theme = "backgroundthemelight";
document.body.className = localStorage.theme;
if (toggleThemeBtn.innerText = document.body.classList.contains("dark")) {
document.getElementById('toggleThemeBtn').appendChild(i);
} else {
document.getElementById('toggleThemeBtn').appendChild(t);
}
toggleThemeBtn.onclick = () => {
document.body.classList.toggle("dark");
var i = document.createElement("i");
i.className = "fa-solid fa-sun nav-li-icon-bottom";
var t = document.createElement("i");
t.className = "fa-solid fa-moon nav-li-icon-bottom";
if (toggleThemeBtn.innerText = document.body.classList.contains("dark")) {
document.getElementById('toggleThemeBtn').appendChild(i);
} else {
document.getElementById('toggleThemeBtn').appendChild(t);
}
localStorage.theme = document.body.className || "backgroundthemelight"
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
min-height: calc(100vh - 100px);
margin-top: 100px;
}
body.dark {
background: #303133;
color: #fff;
}
body.dark .content {
box-shadow: 0 0 5px #1d2225;
}
body.dark button {
box-shadow: 0 0 5px #1d2225;
color: #fff;
}
body.dark button:hover {
box-shadow: 0 0 10px #1d2225;
}
body.dark button:active {
box-shadow: inset 0 0 5px #1d2225;
}
.content {
width: 55%;
margin: auto;
box-shadow: 0 0 3px silver;
padding: 20px;
height: calc(100vh - 100px - 40px);
}
section:not(:first-child) {
margin-top: 30px;
}
.desc {
margin: 10px 0;
opacity: .7;
}
button {
padding: 10px 20px;
border: none;
background: transparent;
box-shadow: 0 0 5px silver;
cursor: pointer;
outline: none;
}
button:hover {
box-shadow: 0 0 10px silver;
}
button:active {
box-shadow: inset 0 0 5px silver;
}
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<main class="content">
<i class="fa-solid fa-sun nav-li-icon-bottom" id="moodBW"></i>
<i class="fa-solid fa-moon nav-li-icon-bottom"></i>
<section>
<h1>BLock</h1>
<p class="desc">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Officiis quod porro voluptate minima quasi a mollitia dicta corrupti nesciunt! Corrupti, quis at? Perspiciatis voluptatibus provident laboriosam vitae voluptatem autem, cupiditate esse nihil est! Ducimus non, corrupti totam quisquam quod et!
</p>
<button id="toggleThemeBtn">Сменить тему на тёмную</button>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
我在楼上展示了我的旧代码,但在这里它再次不起作用我不知道为什么我附上 CodePen 的链接
url codepen 或 https://codepen.io/SergiuC121/pen/zYPwLRz
IMG 结果: (按第二个按钮后突然第一个按钮)
主题保存在本地存储中,因为我无法在此处执行我的 sn-p。
有什么建议吗?
p.s:我用谷歌翻译把它翻译成英文,如果有人发现错误,请纠正他们,以便其他人理解,谢谢..
【问题讨论】:
标签: javascript html css