小编Bub*_*bly的帖子

正文 CSS 未使用 .setProperty() 进行更新

我正在尝试在网站上启用暗模式/亮模式功能,其中我使用该.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)

html javascript css toggle darkmode

5
推荐指数
1
解决办法
3296
查看次数

标签 统计

css ×1

darkmode ×1

html ×1

javascript ×1

toggle ×1