JavaScript 可透過元素的 style 屬性直接修改行內樣式,例如寬度、高度或顯示狀態。若需要讀取計算後的樣式,則可使用 getComputedStyle()。
style
getComputedStyle()
在跨瀏覽器寫法中,舊版 IE 可能使用 currentStyle,現代瀏覽器則多使用 defaultView.getComputedStyle()。封裝成共用函數後,能讓樣式讀取更一致。
currentStyle
defaultView.getComputedStyle()