我尝试运行我的代码,但是它不起作用

我已经尝试使用“颜色”变量,使用rgb(x,x,x)代码,#xxxx,不带引号...

var thehours = new Date().getHours();
if (thehours >= 0 && thehours < 12) {
     document.body.style.backgroundColor = "white";
} else if (thehours >= 12 && thehours < 18) {
    document.body.style.backgroundColor = "grey";
} else if (thehours >= 18 && thehours < 24) {
    document.body.style.backgroundColor = "black";
}


在控制台中,我收到此消息:“ Uncaught TypeError:在design.js:3处无法读取null的'style'属性”

最佳答案

这应该可以,但是可能在错误的地方被调用,应该在页面加载后调用,我建议将其放在您身体末端的标签中,或者您可以使用按钮并将其放在函数中

10-08 07:23