Using CSS !important with JavaScript

前端 未结 3 724
不知归路
不知归路 2021-02-20 01:35



        
3条回答
  •  梦如初夏
    2021-02-20 01:53

    To override an important style in the style sheet you need to set the style attribute with js:

    function myFunction() {
      var x = document.querySelectorAll("#testDiv p.example");
      x[0].setAttribute('style', 'background-color: red !important');
    }
    p.example {
      background-color: blue !important;
    }

    A heading with class="example"

    A paragraph with class="example".

提交回复
热议问题