I wanted to build a Javascript code which changes the background color of the body when a button is clicked.(我想构建一个Javascript代码,当单击一个按钮时,它会更改主体的背景颜色。)
The console says that body.style.background
is not a function.(控制台说body.style.background
不是函数。) Can you tell what was the mistake in my code?(您能告诉我代码中的错误吗?)
var btn = document.querySelector("button");
var body = document.querySelector("body");
var colors = ["red", "blue", "green", "yellow", "orange", "pink"];
var i = 0;
btn.addEventListener("click", function(){
if (i < colors.length) {
i++;
} else if (i == colors.length) {
i = 0;
}
body.style.background("colors[i]");
});
ask by AKHIL REDDY translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…