DIV CSS 佈局教程網

JS 取得 CSS 樣式:style、currentStyle 與 getComputedStyle
編輯:關於JavaScript     

CSS 樣式分三類:

  • 內嵌樣式:寫在標籤的 style 屬性裡,只對該標籤有效。
  • 內部樣式:寫在 HTML 的 <style> 區塊中,只對當前網頁有效。
  • 外部樣式表:抽到獨立 .css 檔,讓多個頁面共用。

而 JS 要讀取樣式時,可用的介面有三個:

  • element.style:只能拿到內嵌樣式,其他情況回傳空物件。
  • element.currentStyle:IE 專屬,可拿到非內嵌樣式。
  • window.getComputedStyle(element, 偽類):標準做法,可拿到最終計算結果。

其中 getComputedStyle 回傳的是一個 CSSStyleDeclaration 物件,裡頭是該元素所有「最終採用」的屬性值;document.defaultView.getComputedStyle() 是等價寫法。要補充的是,Gecko 2.0(Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)之前第二個參數是必填的,非偽類要傳 null,現在則可省略。

先看一個包含兩種樣式的頁面,#tag 是內嵌樣式、#test 是內部樣式:

 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #test{ width:500px; height:300px; background-color:#CCC; float:left; } </style> </head> <body> <div id = "test"></div> <div id = "tag" style = "width:500px; height:300px;background-color:pink;"></div> </body> </html> 

JS 驗證部分:

 <script type = "text/javascript"> window.onload = function(){ var test = document.getElementById("test"); var tag = document.getElementById("tag"); //CSS樣式對像:CSS2Properties{},CSSStyleDeclaration console.log(test.style); //火狐返回空對像CSS2Properties{},谷歌返回空對像CSSStyleDeclaration{} console.log(tag.style); //返回CSS2Properties{width:"500px",height:"300px",background-color:"pink"} //element.style獲取的是內嵌式的style,如果不是內嵌式,則是一個空對像 console.log(tag.style.backgroundColor);//pink console.log(tag.style['background-color']);//pink //獲取類似background-color,border-radius,padding-left類似樣式的兩種寫法 console.log(test.currentStyle) //火狐和谷歌為Undefined,IE返回CSS對像 console.log(window.getComputedStyle(test,null))//谷歌返回CSSStyleDeclaration{……} ,火狐返回CSS2Properties{……} console.log(window.getComputedStyle(test)) //效果同上,但是在Gecko 2.0 之前,第二個參數"偽類"是必需的 console.log(test.currentStyle.width);//500px(IE) console.log(window.getComputedStyle(test).width); //500px; console.log(window.getComputedStyle(test)['width']);//500px; } </script> 

為了跨瀏覽器方便使用,可以包一個小函式:

 function getStyle(element, attr){ if(element.currentStyle){ return element.currentStyle[attr]; }else{ return window.getComputedStyle(element,null)[attr]; } } console.log(getStyle(test,"cssFloat"));//left console.log(getStyle(test,"float")); //left,早前FF和chrome需要使用cssFloat,不過現在已經不必 console.log(getStyle(test,"stylefloat"));//火狐和谷歌都是undefined console.log(getStyle(test,"styleFloat")); //IE9以下必須使用styleFloat,IE9及以上,支持styleFloat和cssFloat console.log(window.getComputedStyle(test).getPropertyValue("float")) 

上面這個 float 就是經典案例:IE 早期要用 styleFloat,舊版 Firefox 與 Chrome 用 cssFloat,如今大家已統一支援 float。與其去背這些差異,不如改用兩個更通用的方法:

getPropertyValue(IE9 與其他瀏覽器)

 window.getComputedStyle(element, null).getPropertyValue("float"); element.currentStyle.getPropertyValue("float"); 

它不支援駝峰寫法,必須用 "background-color" 這種連字號形式。

getAttribute(IE6~8)

 element.currentStyle.getAttribute("float");//不再需要寫成styleFloat element.currentStyle.getAttribute("backgroundColor");//屬性名需駝峰,否則IE6不支援 

把這兩招搭配起來,就能用同一套邏輯應對所有瀏覽器,不必再記各家的屬性名差異。以上就是這次的整理,希望對大家有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved