我有一个程序,应该按下按钮开始改变它的背景颜色(它不是一个按钮,它是一个<h1>),但当我按下它时,背景不会改变.它应该因为变量的变化而改变,但除此之外,我认为其余的都有效.
var onf = false;
setInterval(timer(), 100);
function startUp(){
document.getElementById("Temmie").textContent = "Hi";
turnOn();
}
function turnOn(){
document.getElementById("Tem").style.color = 'Chartreuse';
document.getElementById("Temmie").style.fontFamily = 'Papyrus';
document.getElementById("Temmie").style.fontSize = '24px';
onf = true;
}
function timer(){
if(onf == true){
t = Math.floor(Math.random() * 3) + 1;
changeBackground();
}
}
function changeBackground(){
if(t == 1){
document.body.style.background = 'ForestGreen';
}
if(t == 2){
document.body.style.background = 'DarkGreen';
}
if(t == 3){
document.body.style.background = 'SteelBlue';
}
}
Run Code Online (Sandbox Code Playgroud)
"Tem"和"Temmie"标签连接到a
和a,但除此之外,这应该足够了.