Get the computed style and omit defaults

前端 未结 6 874
旧巷少年郎
旧巷少年郎 2020-12-28 19:57

I\'m trying to get the current runtime style of an element and filter out properties that have default values. For example, with markup like this:



        
6条回答
  •  萌比男神i
    2020-12-28 20:43

    Here's an option if you need to use getPropertyValue afterwards, like here

    var computedStyles = getComputedStyles(document.body);
    var propertyValue = computedStyles.getPropertyValue("prop-name");
    

    Use this function:

    function getDefaultComputedStyles(el)
    {
        var temp = document.createElement("div");
        document.body.appendChild(temp);
        var defaultStyles = getComputedStyle(temp);
        var extraStyles = getComputedStyle(el);
        var foundStyles = [];
    
        for(var i=0; i (`${ind}` in el));
            return result[0]!=undefined ? result[0][Object.keys(result[0])] : null;
        }
    
        return foundStyles;
    }
    

提交回复
热议问题