原生JS添加样式 & 内联important

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');

更多参考下面 ↓

原文链接:https://www.cnblogs.com/webpon/p/13432184.html


版权声明:本文为shihezhengshz原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。