How do you read CSS rule values with JavaScript?

后端 未结 16 1155
天涯浪人
天涯浪人 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: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' )
                }
            }
        }
    
    }
    

提交回复
热议问题