<div id="testDiv"> <h2 class="example">A heading with class="example"</h2> <p class="example">A pa
... . .
<div id="testDiv"> <h2 class="example">A heading with class="example"</h2> <p class="example">A paragraph with class="example".</p></div><button onclick="myFunction()">Try it</button><style> .example { background-color: green !important; }</style><script> function myFunction() { var x = document.querySelectorAll("#testDiv p.example"); x[0].style.backgroundColor = "red"; }</script>
从上面的代码中,我如何在脚本标记中定义的JS代码中覆盖上述样式属性中的!important的css属性?
注意:我们有一些内部应用程序声明其样式很重要
.解决方法
. 试试这个:
function myFunction() { var x = document.querySelectorAll("#testDiv p.example"); x[0].style.setProperty("background-color","red","important");}. . .. ...