实际应用中,我们经常需要获取dom结点的样式,我们可以使用element.style很轻松的获取到dom的内联样式,但是要通过这种方式获取外部和内部样式就无可奈何了。
还好我们有getComputedStyle()这个方法,此方法在IE9下的不兼容,但可以通过element.currentStyle的方式变相获取。
如下就是完整的JS获取dom所有样式的方法:
function getEleStyle(ele,attr){
var style = window.getComputedStyle ?window.getComputedStyle(ele):ele.currentStyle;
var css = attr ? window.getComputedStyle(ele)[attr]:window.getComputedStyle(ele);
return css;
}