1. getComputedStyle介绍
getComputedStyle:是一个可以获取当前元素所有最终使用的css属性值,同时也会获取该元素的默认css属性,返回的是一个css样式声明对象,只读。
语法:(IE9一下不支持,因此需要给出兼容写法)
var computed = window.getComputedStyle("元素", "伪类"); //第一个参数为必填参数,第二个参数如果没有伪类可以使用null
2. getCurrentStyle介绍
getCurrentStyle:和getComputedStyle 方法类似,都是可以获取元素的所有css属性(包括默认的属性),区别在于getCurrentStyle仅在IE中支持,返回的也是一个对象,只读
语法
var current = element.currentStyle("属性");//
3. getComputedStyle与getCurrentStyle的兼容写法
function getStyle( obj, attr ){
return window.getComputedStyle ? window.getComputedStyle(obj,attr).attr : obj.currentStyle.attr;
}