1.直接设置style——无法设置important
单个设置:适用于IE系列,样式名需要小驼峰书写
element.style.backgroundColor = "#ccc"
批量设置:样式怎么写,你就怎么写,不适用于IE系列
element.style = "width:100px;background-color:#ccc;"
2.通过设置属性,设置style——可以设置important
样式怎么写,你就怎么写
但这种方式通过设置属性去设置样式不推荐,毕竟会覆盖之前的js设置的样式
element.setAttribute('style','width:100px;background-color:#ccc !important;')
3.使用setProperty函数——可以设置important
单个设置,无需小驼峰
div.style.setProperty('background-color', '#ccc', 'important');