if和else if语句被忽略,Javascript

时间:2020-06-05 14:28:14

标签: javascript if-statement

我有一个紫色按钮。即使在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";
        }
    }`

2 个答案:

答案 0 :(得分:2)

此符号=赋值,而不是比较if (m.style.backgroundColor="yellow")应该是if (m.style.backgroundColor === "yellow")

答案 1 :(得分:0)

使用比较运算符if(m.style.backgroundColor ===“ yellow”)代替if(m.style.backgroundColor =“ yellow”)