Changing a CSS rule-set from Javascript

前端 未结 8 1004
甜味超标
甜味超标 2020-11-22 04:54

Is it possible to make changes to a CSS rule-set dynamically (i.e. some JS which would change a CSS rule-set when the user clicks a widget)

This particular CSS rule-

8条回答
  •  一向
    一向 (楼主)
    2020-11-22 05:56

    You can edit CLASS in document styleshets as follows

    [...document.styleSheets[0].cssRules].find(x=> x.selectorText=='.box')
         .style.background= 'red';
    

    function edit() {
      [...document.styleSheets[0].cssRules].find(x=> x.selectorText=='.box')
        .style.background= 'red';
    }
    .box {
      margin: 10px;
      padding: 10px;
      background: yellow;
    }
    
    
    My box 1
    My box 2
    My box 3

提交回复
热议问题