Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中更改div的backgroundcolor属性?_Javascript_Css - Fatal编程技术网

如何在JavaScript中更改div的backgroundcolor属性?

如何在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

我正在尝试更改div的背景色,这取决于使用我的网站的人回答的问题是正确的(绿色),还是错误的(红色)。我使用的是条件语句和提示问题。 所以基本上我有一个代码,看起来像这样

      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吗?我没有,但现在我看到它起作用了。谢谢就这样!谢谢