How do you read CSS rule values with JavaScript?

后端 未结 16 1167
天涯浪人
天涯浪人 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:20

    Adapted from here, building on scunliffe's answer:

    function getStyle(className) {
        var cssText = "";
        var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
        for (var x = 0; x < classes.length; x++) {        
            if (classes[x].selectorText == className) {
                cssText += classes[x].cssText || classes[x].style.cssText;
            }         
        }
        return cssText;
    }
    
    alert(getStyle('.test'));
    
    0 讨论(0)
  • 2020-11-21 21:20

    Some browser differences to be aware of:

    Given the CSS:

    div#a { ... }
    div#b, div#c { ... }
    

    and given InsDel's example, classes will have 2 classes in FF and 3 classes in IE7.

    My example illustrates this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <style>
        div#a { }
        div#b, div#c { }
        </style>
        <script>
        function PrintRules() {
        var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules
            for(var x=0;x<rules.length;x++) {
                document.getElementById("rules").innerHTML += rules[x].selectorText + "<br />";
            }
        }
        </script>
    </head>
    <body>
        <input onclick="PrintRules()" type="button" value="Print Rules" /><br />
        RULES:
        <div id="rules"></div>
    </body>
    </html>
    
    0 讨论(0)
  • 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<classes.length;x++) {
                    if(classes[x].selectorText===className) {
                        return  (classes[x].cssText ? classes[x].cssText : classes[x].style.cssText);
                    }
                }
            }
            return false;
        };
    
    0 讨论(0)
  • 2020-11-21 21:23

    I added return of object where attributes are parsed out style/values:

    var getClassStyle = 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<classes.length;x++) {
                if(classes[x].selectorText===className){
                    classStyleTxt = (classes[x].cssText ? classes[x].cssText : classes[x].style.cssText).match(/\{\s*([^{}]+)\s*\}/)[1];
                    var classStyles = {};
                    var styleSets = classStyleTxt.match(/([^;:]+:\s*[^;:]+\s*)/g);
                    for(y=0;y<styleSets.length;y++){
                        var style = styleSets[y].match(/\s*([^:;]+):\s*([^;:]+)/);
                        if(style.length > 2)
                            classStyles[style[1]]=style[2];
                    }
                    return classStyles;
                }
            }
        }
        return false;
    };
    
    0 讨论(0)
  • 2020-11-21 21:26

    I faced the same problem. And with the help of guys I came up with a really smart solution that solve that problem totally (run on chrome ) .

    Extract all images from the network

     function AllImagesUrl (domain){
      return  performance.getEntries()
        .filter( e=> 
           e.initiatorType == "img" &&
           new RegExp(domain).test(e.name) 
        )
      .map( e=> e.name.replace('some cleaning work here','') ) ```
    
    0 讨论(0)
  • 2020-11-21 21:26

    Based on @dude answer this should return relevant styles in a object, for instance:

    .recurly-input {                                                                                                                                                                             
      display: block;                                                                                                                                                                            
      border-radius: 2px;                                                                                                                                                                        
      -webkit-border-radius: 2px;                                                                                                                                                                
      outline: 0;                                                                                                                                                                                
      box-shadow: none;                                                                                                                                                                          
      border: 1px solid #beb7b3;                                                                                                                                                                 
      padding: 0.6em;                                                                                                                                                                            
      background-color: #f7f7f7;                                                                                                                                                                 
      width:100%;                                                                                                                                                                                
    }
    

    This will return:

    backgroundColor:
    "rgb(247, 247, 247)"
    border
    :
    "1px solid rgb(190, 183, 179)"
    borderBottom
    :
    "1px solid rgb(190, 183, 179)"
    borderBottomColor
    :
    "rgb(190, 183, 179)"
    borderBottomLeftRadius
    :
    "2px"
    borderBottomRightRadius
    :
    "2px"
    borderBottomStyle
    :
    "solid"
    borderBottomWidth
    :
    "1px"
    borderColor
    :
    "rgb(190, 183, 179)"
    borderLeft
    :
    "1px solid rgb(190, 183, 179)"
    borderLeftColor
    :
    "rgb(190, 183, 179)"
    borderLeftStyle
    :
    "solid"
    borderLeftWidth
    :
    "1px"
    borderRadius
    :
    "2px"
    borderRight
    :
    "1px solid rgb(190, 183, 179)"
    borderRightColor
    :
    "rgb(190, 183, 179)"
    borderRightStyle
    :
    "solid"
    borderRightWidth
    :
    "1px"
    borderStyle
    :
    "solid"
    borderTop
    :
    "1px solid rgb(190, 183, 179)"
    borderTopColor
    :
    "rgb(190, 183, 179)"
    borderTopLeftRadius
    :
    "2px"
    borderTopRightRadius
    :
    "2px"
    borderTopStyle
    :
    "solid"
    borderTopWidth
    :
    "1px"
    borderWidth
    :
    "1px"
    boxShadow
    :
    "none"
    display
    :
    "block"
    outline
    :
    "0px"
    outlineWidth
    :
    "0px"
    padding
    :
    "0.6em"
    paddingBottom
    :
    "0.6em"
    paddingLeft
    :
    "0.6em"
    paddingRight
    :
    "0.6em"
    paddingTop
    :
    "0.6em"
    width
    :
    "100%"
    

    Code:

    function getStyle(className_) {
    
        var styleSheets = window.document.styleSheets;
        var styleSheetsLength = styleSheets.length;
        for(var i = 0; i < styleSheetsLength; i++){
            var classes = styleSheets[i].rules || styleSheets[i].cssRules;
            if (!classes)
                continue;
            var classesLength = classes.length;
            for (var x = 0; x < classesLength; x++) {
                if (classes[x].selectorText == className_) {
                    return _.pickBy(classes[x].style, (v, k) => isNaN(parseInt(k)) && typeof(v) == 'string' && v && v != 'initial' && k != 'cssText' )
                }
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题