if和else if语句被忽略,Javascript [关闭]

问题描述 投票:0回答:2

我有一个紫色按钮。即使在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";
        }
    }`
javascript if-statement
2个回答
2
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.