<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>
从上面的代码中,我应该如何在script标签中定义的JS代码中使用!important覆盖上面的style属性中的css属性?
注意:我们有一些内部应用程序声明了important样式。
style.backgroundColor = "red !important"
不起作用吗? - evolutionxbox