how to make a background-color on a div tag

后端 未结 2 1121
礼貌的吻别
礼貌的吻别 2021-01-29 06:54

I have this code:

 
相关标签:
2条回答
  • 2021-01-29 07:15

    You're looking for the style property on the element, there is no background-color attribute:

    document.getElementById("pic1").style.backgroundColor = "red";
    

    That said, generally best to style things with CSS and add/remove classes.

    0 讨论(0)
  • 2021-01-29 07:32

    T.J. Crowder beat me to it, but just as a demo, if you were hellbent on using setAttribute, you could do so like this:

    document.getElementById("pic1").setAttribute("style","background-color:red");
    

    As you see, the css attribute is background-color, not the html attribute.

    0 讨论(0)
提交回复
热议问题