我有一个紫色按钮。即使在if语句而不是紫色中,它也会变成绿色的onclick,即使我写了Yellow。 然后,它保持绿色,尽管else if语句应在再次单击时将其再次变为紫色。 我的代码有什么问题?
`var m = document.getElementById ("buttonanimation");
function changecolor(){
if (m.style.backgroundColor="yellow"){
m.style.backgroundColor="green";
} else if (m.style.backgroundColor="green"){
m.style.backgroundColor="purple";
}
}`
答案 0 :(得分:2)
此符号=
是赋值,而不是比较。 if (m.style.backgroundColor="yellow")
应该是if (m.style.backgroundColor === "yellow")
。
答案 1 :(得分:0)
使用比较运算符if(m.style.backgroundColor ===“ yellow”)代替if(m.style.backgroundColor =“ yellow”)