1. 程式人生 > >JS獲取CSS樣式(style/getComputedStyle/currentStyle)

JS獲取CSS樣式(style/getComputedStyle/currentStyle)

CSS的樣式分為三類:
內嵌樣式:是寫在Tag裡面的,內嵌樣式只對所有的Tag有效。
內部樣式:是寫在HTML的裡面的,內部樣式只對所在的網頁有效。
外部樣式表:如果很多網頁需要用到同樣的樣式(Styles),將樣式(Styles)寫在一個以.css為字尾的CSS檔案裡,然後在每個需要用到這 些樣式(Styles)的網頁裡引用這個CSS檔案。

getComputedStyle是一個可以獲取當前元素所有最終使用的CSS屬性值。返回的是一個CSS樣式物件([object CSSStyleDeclaration])
currentStyle是IE瀏覽器的一個屬性,返回的是CSS樣式物件

element指JS獲取的DOM物件


element.style //只能獲取內嵌樣式
element.currentStyle //IE瀏覽器獲取非內嵌樣式
window.getComputedStyle(element,偽類) //非IE瀏覽器獲取非內嵌樣式
document.defaultView.getComputedStyle(element,偽類)//非IE瀏覽器獲取非內嵌樣式
注:Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 之前,第二個引數“偽類”是必需的(如果不是偽類,設定為null),現在可以省略這個引數。

下面的html中包含兩種css樣式,id為tag的div是內嵌樣式,而id為test的div樣式為內部樣式.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <!doctype html> < html lang = "en" >    < head >      < meta charset = "UTF-8" >      < meta name = "Generator" content = "EditPlus®" >      < meta name = "Author" content = "Yvette Lau" >      < meta name = "Keywords" content = "關鍵字" >      < meta name = "Description" content = "描述" >      < 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部分

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <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 (Firefox 4/Thunderbird 3.3/SeaMonkey 2.1) 之前,第二個引數“偽類”是必需的(如果不是偽類,設定為null)        console.log(test.currentStyle.width); //500px(IE)      console.log(window.getComputedStyle(test).width); //500px;      console.log(window.getComputedStyle(test)[ 'width' ]); //500px;      //document.defaultView.getComputedStyle(element,null)[attr]/window.getComputedStyle(element,null)[attr]      } </script>

以上的例子僅是驗證前面的論述是否正確。

為了簡單,我們也可以對獲取樣式做一個簡單的封裝。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 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,而早前的FF和chrome使用的是cssFloat,現在FF和Chrome已經支援float,還有一些其他的屬性,不再一一列出,為了不去記憶這些差異點,我們引出兩個訪問CSS樣式物件的方法:
getPropertyValue方法和getAttribute方法

IE9及其它瀏覽器(getPropertyValue)
window.getComputedStyle(element, null).getPropertyValue(“float”);
element.currentStyle.getPropertyValue(“float”);
getPropertyValue不支援駝峰寫法。(相容IE9及以上,FF,Chrom,Safari,Opera)
如:window.getComputedStyle(element,null).getPropertyValue(“background-color”);

對於IE6~8,需要使用getAttribute方法,用於訪問CSS樣式物件的屬性

element.currentStyle.getAttribute(“float”);//不再需要寫成styleFloat
element.currentStyle.getAttribute(“backgroundColor”);//屬性名需要寫成駝峰寫法,否則IE6不支援,如果無視IE6,可以寫成”background-color”