首頁 > web前端 > js教程 > 主體

JS取得CSS樣式(style/getComputedStyle/currentStyle)_javascript技巧

WBOY
發布: 2016-05-16 15:19:19
原創
1215 人瀏覽過

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&reg;">
    <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); //火狐返回空对象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>

登入後複製

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

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

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”

以上就是本文的全部內容,希望對大家的學習有所幫助,輕鬆使用JS取得CSS樣式。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!