How can I conditionally change css styles with js?

前端 未结 2 1233
生来不讨喜
生来不讨喜 2021-01-06 22:35

I\'d like to implement some javascript that uses an if statement to change some css styles. Not sure how to do it, any help would be great!

2条回答
  •  爱一瞬间的悲伤
    2021-01-06 22:56

    First of all, to change CSS using JavaScript, the syntax looks like the following:

    document.getElementById(id).style.property = new style
    

    For example, if you want to change the display property of an element with id = "container" to block, it would be:

    document.getElementById("container").style.display = "block";
    

    Given this, you could easily add an IF statement depending on what condition you want. For example:

    if(condition)
    {
         document.getElementById("container").style.display = "block";
    }
    

提交回复
热议问题