How to get the applied style from an element, excluding the default user agent styles

前端 未结 4 1540
后悔当初
后悔当初 2020-12-10 04:55

How in JavaScript do you retrieve the styles that have been applied to an element, excluding the default user agent styles (so inline + stylesheet styles on

4条回答
  •  旧巷少年郎
    2020-12-10 05:32

    There is a read only property of document called 'styleSheets'.

    var styleSheetList = document.styleSheets;
    

    https://developer.mozilla.org/en-US/docs/Web/API/Document/styleSheets

    By using this, you can reach all the styles which are applied by the author.

    There is a similar question about this but not a duplicate, in here:

    Is it possible to check if certain CSS properties are defined inside the style tag with Javascript?

    You can get the applied style from an element, excluding the default user agent styles using the accepted answer of that question i just mentioned.

    That answer didn't supply the element's own style attribute content, so i have improved the code a bit:

    var proto = Element.prototype;
    var slice = Function.call.bind(Array.prototype.slice);
    var matches = Function.call.bind(proto.matchesSelector || 
                    proto.mozMatchesSelector || proto.webkitMatchesSelector ||
                    proto.msMatchesSelector || proto.oMatchesSelector);
    
    // Returns true if a DOM Element matches a cssRule
    var elementMatchCSSRule = function(element, cssRule) {
      return matches(element, cssRule.selectorText);
    };
    
    // Returns true if a property is defined in a cssRule
    var propertyInCSSRule = function(prop, cssRule) {
      return prop in cssRule.style && cssRule.style[prop] !== "";
    };
    
    // Here we get the cssRules across all the stylesheets in one array
    var cssRules = slice(document.styleSheets).reduce(function(rules, styleSheet) {
      return rules.concat(slice(styleSheet.cssRules));
    }, []);
    
    
    
    
    var getAppliedCss = function(elm) {
    	// get only the css rules that matches that element
    	var elementRules = cssRules.filter(elementMatchCSSRule.bind(null, elm));
    	var rules =[];
    	if(elementRules.length) {
    		for(i = 0; i < elementRules.length; i++) {
    			var e = elementRules[i];
    			rules.push({
    				order:i,
    				text:e.cssText
    			})
    		}		
    	}
    	
    	if(elm.getAttribute('style')) {
    		rules.push({
    				order:elementRules.length,
    				text:elm.getAttribute('style')
    			})
    	}
    	return rules;
    }
    
    
    
    
    
    
    
    function showStyle(){
    var styleSheetList = document.styleSheets;
    // get a reference to an element, then...
    var div1 = document.getElementById("div1");
    
    var rules = getAppliedCss(div1);
    
    var str = '';
    for(i = 0; i < rules.length; i++) {
    			var r = rules[i];
    			str += '
    Style Order: ' + r.order + ' | Style Text: ' + r.text; } document.getElementById("p1").innerHTML = str; }
    #div1 {
    float:left;
    width:100px;
    }
    
    div {
    text-align:center;
    }
    Lorem ipsum


    Show me the style.

提交回复
热议问题