我正在尝试在测验网站中实施夜间模式。
当我加载页面时,它说无法读取未定义的属性'addEventListener'。我尝试如下
HTML
<button class="js--toggle-lights">Change Theme</button>
脚本
var d = document.documentElement,
t = document.querySelectorAll(".js--toggle-lights")[0],
m = localStorage.getItem("nightmode");
if(m == "true") {
d.classList.add("night-mode");
}
t.addEventListener("click", function(){
if(d.classList.contains("night-mode")) {
d.classList.remove("night-mode");
localStorage.setItem("nightmode", "false");
}else {
d.classList.add("night-mode");
localStorage.setItem("nightmode", "true");
}
});
样式
.js--toggle-lights {
border: 1px solid currentColor;
color: inherit;
}
.night-mode {
background: #000;
color: #fff;
}