How do you read CSS rule values with JavaScript?

后端 未结 16 1154
天涯浪人
天涯浪人 2020-11-21 21:02

I would like to return a string with all of the contents of a CSS rule, like the format you\'d see in an inline style. I\'d like to be able to do this without knowing what i

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 21:21

    This version will go through all of the stylesheets on a page. For my needs, the styles were usually in the 2nd to last of the 20+ stylesheets, so I check them backwards.

        var getStyle = function(className){
            var x, sheets,classes;
            for( sheets=document.styleSheets.length-1; sheets>=0; sheets-- ){
                classes = document.styleSheets[sheets].rules || document.styleSheets[sheets].cssRules;
                for(x=0;x

提交回复
热议问题