首页 > 代码库 > getComputedStyle currentStyle 获取当前元素所有最终使用的CSS属性值

getComputedStyle currentStyle 获取当前元素所有最终使用的CSS属性值

object.getComputedStyle  获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式声明对象,只读,不能设置。

    此方法不兼容IE8及以下,需用currentStyle方法。

function getStyle(obj.attr){
    return getComputedStyle(obj)?getComputedStyle(obj)[attr]:obj.currentStyle[attr]; //判断是否有getComputedStyle方法 
}

 

obj为需要获取属性值的对象,attr则为需要获取到的属性。使用时直接调用getStyle函数即可。注意getComputedStyle和currentStyle使用方法有区别。

 

 

getComputedStyle currentStyle 获取当前元素所有最终使用的CSS属性值