我在我的主页上制作了一个实时时钟。现在我想在 6 点钟之后将 #nav div 的颜色更改为黑色。我有这段代码,但它不起作用。时钟改变了,但背景颜色没有改变
function maakTijd() {
var Tijd = new Date();
var h = Tijd.getHours();
var m = Tijd.getMinutes();
var s = Tijd.getSeconds();
setTimeout('maakTijd()',1000);
if (h < 10){
h = "0"+ h;
}
if (m < 10){
m = "0"+ m;
}
if (s < 10){
s = "0"+ s;
}
var klok = document.getElementById('hometext');
klok.textContent = h + ":" + m + ":" + s;
klok.innerText = h + ":" + m + ":" + s;
// the code above is the clock
if (h > 18 && h < 6) //here i set the time and the if statement
{
document.getElementById("hometext").style.backgroundColor = "black";
}
}
maakTijd();
最佳答案
h 不能同时大于 18 且小于 6...
而不是这个:
if (h > 18 && h < 6)
使用这个:
if (h > 18 || h < 6)
您可能需要 Or 而不是 And。
关于javascript - 我想改变 javascript 时间相关的 div 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852848/