How to get CSS class by name from StyleSheet?

后端 未结 3 1827
忘掉有多难
忘掉有多难 2021-01-15 22:39

I have the following code which uses the index to get stylesheet and also css class inside that stylesheet.

 for (var s = document.styleSheets.length - 1; s         


        
3条回答
  •  醉话见心
    2021-01-15 23:19

    If I understands correctly, you want to change CSS. Precisely you want to change CSS rules for a class. To do so, and to be the most performance efficient, I would create a stylesheet with your selectors, it might be just .selector>div{} for example or just .selector{}.

    
    

    I used this a while ago to make my table cells square (while my cells was all equal).

    Then I used this code to change parameters of the class in the stylesheet.

    document.styleSheets[0].cssRules[numberOfSelector].style.height=document.querySelector('.squerify td').offsetWidht+"px";
    

    This was the best way I could think of, but if you want, you could use a loop to search for the classes.
    Send a comment if misunderstood your question.

提交回复
热议问题