来自:蓝色天空css
样式表有三种方式spa
最经常使用的是style属性,在JavaScript中,经过document.getElementById(id).style.XXX就能够获取到XXX的值,但意外的是,这样作只能取到经过内嵌方式设置的样式值,即style属性里面设置的值。firefox
style 标准的样式!多是由style属性指定的!
runtimeStyle 运行时的样式!若是与style的属性重叠,将覆盖style的属性!
currentStyle 指 style 和 runtimeStyle 的结合!code
经过currentStyle就能够获取到经过内联或外部引用的CSS样式的值了(仅限IE)blog
如:document.getElementById("test").currentStyle.topip
要兼容FF,就得须要getComputedStyle 出马了get
注意:getComputedStyle是firefox中的,it
currentStyle是ie中的.class
例:test
<style> #mydiv { width : 300px; } </style>
var mydiv = document.getElementById('mydiv'); if(mydiv.currentStyle) { var width = mydiv.currentStyle['width']; alert('ie:' + width); } else if(window.getComputedStyle) { var width = window.getComputedStyle(mydiv , null)['width']; alert('firefox:' + width); }
另外在FF下还能够经过下面的方式获取
document.defaultView.getComputedStyle(mydiv,null).width window.getComputedStyle(mydiv , null).width