Listing known CSS classes using Javascript

前端 未结 5 2029
傲寒
傲寒 2020-12-19 00:56

I\'m trying to find a good way to collect the names of classes defined in the stylesheets included with a given document. I know about document.StyleSheetList b

5条回答
  •  甜味超标
    2020-12-19 01:14

    This will show all rules defined in the stylesheets.

    var allRules = [];
    var sSheetList = document.styleSheets;
    for (var sSheet = 0; sSheet < sSheetList.length; sSheet++)
    {
        var ruleList = document.styleSheets[sSheet].cssRules;
        for (var rule = 0; rule < ruleList.length; rule ++)
        {
           allRules.push( ruleList[rule].selectorText );
        }
    }
    

    The thing, though, is that it includes all rules regardless of being class or tag or id or whatever..

    You will need to explain in more detail what you want to happen for non class rules (or combined rules)

提交回复
热议问题