JS擷取CSS樣式(style/getComputedStyle/currentStyle),style.css樣式表

來源:互聯網
上載者:User

JS擷取CSS樣式(style/getComputedStyle/currentStyle),style.css樣式表

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樣式為內部樣式.

<!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部分

<script type = "text/javascript">  window.onload = function(){    var test = document.getElementById("test");    var tag = document.getElementById("tag");    //CSS樣式對象:CSS2Properties{},CSSStyleDeclaration    console.log(test.style); //Firefox返回Null 物件CSS2Properties{},Google返回Null 物件CSSStyleDeclaration{}     console.log(tag.style); //返回CSS2Properties{width:"500px",height:"300px",background-color:"pink"}    //element.style擷取的是內嵌式的style,如果不是內嵌式,則是一個Null 物件    console.log(tag.style.backgroundColor);//pink    console.log(tag.style['background-color']);//pink    //擷取類似background-color,border-radius,padding-left類似樣式的兩種寫法啊    console.log(test.currentStyle) //Firefox和Google為Undefined,IE返回CSS對象    console.log(window.getComputedStyle(test,null))//Google返回CSSStyleDeclaration{……} ,Firefox返回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>

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

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

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"));//Firefox和Google都是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”

以上就是本文的全部內容,希望對大家的學習有所協助,便於使用JS擷取CSS樣式。

您可能感興趣的文章:
  • 用JavaScript擷取網頁中的js、css、Flash等檔案
  • javascript 擷取特定的 CSS屬性值
  • 擷取css樣式表內樣式的js函數currentStyle(IE),defaultView(FF)
  • js 擷取和設定css3 屬性值的實現方法
  • javascript擷取元素CSS樣式程式碼範例
  • js擷取某元素的class裡面的css屬性值代碼
  • JS使用getComputedStyle()方法擷取CSS屬性值
  • js 擷取瀏覽器版本以此來調整CSS的樣式
  • Javascript擷取CSS虛擬元素屬性的實現代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.