我今天开始编码,我试图制作一个简单的静态黑暗模式按钮,但按钮的背景不会改变其 colored颜色 ,我已经在CSS上第36行修复了一件事,它是--bg: var(--green);,但当我点击(https://i.stack.imgur.com/CYUB4.png)时屏幕会是黑色的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>site</title>
    <link rel="stylesheet" href="main.css">
</head>
<body class="light-theme">
    <h1>Task List</h1>
    <p id="msg">Current tasks:</p>
    <ul>
        <li class="list">Add visual styles</li>
        <li class="list">Add light and dark themes</li>
        <li> Enable switching the theme</li>
    </ul>
    <div>
        <button class="btn">Dark</button>
    </div>
    <script src="app.js" defer></script>
    <noscript>You need to enable JavaScript to view the full site.</noscript>
</body>
</html>

body {
    font-family: monospace;
}
ul {
    font-family: Arial, Helvetica, sans-serif;
}

li {
    list-style: circle;
}

.list {
    list-style: square;
}

.light-theme {
    color: black;
    background: #00FF00;
    
}

:root {
    --green: #00FF00;
    --white: #ffffff;
    --black: #000000;
}

.light-theme {
    --bg: var(--green);
    --fontColor: var(--black);
    --btng: var(--black)
    --btnFontColor: var(--white)
}

.dark-theme {
    background: var(--black);
    --fontColor: var(--green);
    --btnBg: var(--white)
    --btnFontColor: var(--black)
}

* {
    color: var(--fontColor);
    font-family: Arial, Helvetica, sans-serif;
}

body  {
    background: var(--bg);
}

.btn {
    color: var(--btnFontColor);
    background-color: var(--btnBg);
}

.btn {
    position: absolute;
    top: 20px;
    left: 250px;
    height: 50px;
    width: 50px;
    border-radius: 50%;
    border: none;
    color: var(--btnFontColor);
    background-color: var(--btnBg);
}


.btn:focus { outline: none; }




 'use scrict'

const switcher = document.querySelector('.btn');

switcher.addEventListener('click', function() {
    document.body.classList.toggle('dark-theme')

    var className = document.body.className;
    if(className == "light-theme") {
        this.textContent = "Dark";
    }   
    else {
        this.textContent = "Light"
    }

    console.log('current class name: ' + className);
});
enter image description here

它应该是什么样子:(https://i.stack.imgur.com/SGUMf.png)

推荐答案

您忘记在.light-theme和.dark-theme中的CSS属性末尾添加";",并且在.light-theme中您有--btng而不是--btnBg.

.light-theme {
    --bg: var(--green);
    --fontColor: var(--black);
    --btnBg: var(--black);
    --btnFontColor: var(--white);
}

.dark-theme {
background: var(--black);
    --fontColor: var(--green);
    --btnBg: var(--white);
    --btnFontColor: var(--black);
}

Javascript相关问答推荐

为什么子组件没有在reaction中渲染?

如何使用CSS和JavaScript创建粘性、凝聚力的形状到形状(容器)变形?

使用JavaScript重命名对象数组中的键

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

Chart.js V4切换图表中的每个条,同时每个条下有不同的标签.怎么做?

Google图表时间轴—更改hAxis文本 colored颜色

Angular中计算信号和getter的区别

将状态向下传递给映射的子元素

在这种情况下,如何 for each 元素添加id?

无法读取未定义错误的属性路径名''

在forEach循环中获取目标而不是父对象的属性

类构造函数不能在没有用With Router包装的情况下调用

覆盖加载器页面避免对页面上的元素进行操作

无法避免UV:flat的插值:非法使用保留字"

删除加载页面时不存在的元素(JavaScript)

如何在Java脚本中对列表中的特定元素进行排序?

无法检索与Puppeteer的蒸汽游戏的Bundle 包价格

使用可配置项目创建网格

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

rxjs在每次迭代后更新数组的可观察值