來自:藍色天空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