How do you change the style of a div programmatically

后端 未结 8 2547
攒了一身酷
攒了一身酷 2021-02-20 06:56

How do I change the style (color) of a div such as the following?

\"
相关标签:
8条回答
  • 2021-02-20 07:18

    IMO this is the better way to do it. I found some of this in other posts but this one comes up first in google search.

    This part works for standard JavaScript. I am pretty sure you can use it to remove all styles as well as add/overwite.

    var div = document.createElement('div');
    div.style.cssText = "border-radius: 6px 6px 6px 6px; height: 250px; width: 600px";
    

    OR

    var div = document.getElementById('foo');
    div.style.cssText = "background-color: red;";
    

    This works for jQuery only

    $("#" + TDDeviceTicketID).attr("style", "padding: 10px;");
    $("#" + TDDeviceTicketID).attr("class", "roundbox1");
    
    This works for removing it JQUERY
    $("#" + TDDeviceTicketID).removeAttr("style");
    $("#" + TDDeviceTicketID).removeAttr("class");
    
    0 讨论(0)
  • 2021-02-20 07:22

    Generally, you can do it directly

    document.getElementById("myDiv").style.color = "red";

    There's a reference here.

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