我正在尝试在网站上启用暗模式/亮模式功能,其中我使用该.setProperty函数来更改变量的十六进制值。但是,每当我切换开关时,该网站都不会更新。为什么是这样?以下是与该问题相关的所有代码:
let color = "#141414";
function changeMode() {
console.log("Switch was toggled. Switching site color scheme...")
let bg = document.querySelector(':root');
// Should switch the value of 'color' from grey to white, and vice versa
if (color === "#141414") { color = "#ffffff"; } else if (color === "#ffffff") { color = "#141414"; }
bg.style.setProperty("--main2", color);
}Run Code Online (Sandbox Code Playgroud)
:root {
--main1: #ffdb15;
--main2: #141414;
}
html, body {
background-color: var(--main2);
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px; …Run Code Online (Sandbox Code Playgroud)