CSS 有三種常見寫法:內嵌樣式、內部樣式與外部樣式。透過 element.style 只能讀取內嵌在 style 屬性中的值,若樣式來自外部 CSS 或 head 中的 style 區塊,就讀不到。
在 IE 中,可使用 currentStyle 取得目前生效的樣式;在 Firefox 等瀏覽器中,則需使用 window.getComputedStyle 或 document.defaultView.getComputedStyle。若要相容兩者,可先判斷 currentStyle 是否存在,否則改用 getComputedStyle。
runtimeStyle 是執行階段可覆寫的樣式,currentStyle 則是 style 與 runtimeStyle 的綜合結果。理解這三者差異,才能在跨瀏覽器環境中正確讀取寬度、高度與位置等樣式值。