如何在JavaScript中更改div的backgroundcolor属性?
我正在尝试更改div的背景色,这取决于使用我的网站的人回答的问题是正确的(绿色),还是错误的(红色)。我使用的是条件语句和提示问题。 所以基本上我有一个代码,看起来像这样如何在JavaScript中更改div的backgroundcolor属性?,javascript,css,Javascript,Css,我正在尝试更改div的背景色,这取决于使用我的网站的人回答的问题是正确的(绿色),还是错误的(红色)。我使用的是条件语句和提示问题。 所以基本上我有一个代码,看起来像这样 if(q1 === "ubuntu") { div1.innerHTML="Right answer!" counter ++; //here would be the place where I want to put the code that makes
if(q1 === "ubuntu") {
div1.innerHTML="Right answer!"
counter ++;
//here would be the place where I want to put the code that makes another div change it's color
} else {
div.innerHTML="Wrong answer!"
}
所有HTML元素都有一个style属性,因此您可以通过设置
div1.style.color=“#54b37a”
或div1.style.backgroundColor=“#54b37a”
分别更改div的颜色(文本颜色)或背景色,具体取决于您的需要您考虑过div.style.backgroundColor吗?我没有,但现在我看到它起作用了。谢谢就这样!谢谢