js中style,currentStyle和getComputedStyle的区别

1.style只能获取元素的内联样式,内部样式和外部样式是获取不到的。例子:

<div id="test" style="width:100px;height:200px;"></div>

alert(document.getElementById('test').style.height);

2.currentStyle和getComputerStyle可以获取元素的内部样式和外部样式,但是currentStyle只适用于IE,getComputerStyle适用于FF、opera、safari、chrome

下面的方法可以解决兼容性

display = this.currentStyle? this.currentStyle.display: window.getComputedStyle(this, null).display;

display代表css属性,可以是width,color,backgroundSize等

posted @ 2015-01-31 14:37  MissBean  阅读(261)  评论(0编辑  收藏  举报