亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2016年01月19日 16:31:33   作者:YvetteLau  
這篇文章主要為大家介紹了JS獲取CSS樣式的方法,介紹了CSS樣式的三種分類情況,對(duì)獲取樣式做一個(gè)簡(jiǎn)單的封裝,感興趣的小伙伴們可以參考一下

CSS的樣式分為三類:
內(nèi)嵌樣式:是寫在Tag里面的,內(nèi)嵌樣式只對(duì)所有的Tag有效。
內(nèi)部樣式:是寫在HTML的里面的,內(nèi)部樣式只對(duì)所在的網(wǎng)頁(yè)有效。
外部樣式表:如果很多網(wǎng)頁(yè)需要用到同樣的樣式(Styles),將樣式(Styles)寫在一個(gè)以.css為后綴的CSS文件里,然后在每個(gè)需要用到這 些樣式(Styles)的網(wǎng)頁(yè)里引用這個(gè)CSS文件。

getComputedStyle是一個(gè)可以獲取當(dāng)前元素所有最終使用的CSS屬性值。返回的是一個(gè)CSS樣式對(duì)象([object CSSStyleDeclaration])
currentStyle是IE瀏覽器的一個(gè)屬性,返回的是CSS樣式對(duì)象

element指JS獲取的DOM對(duì)象
element.style //只能獲取內(nèi)嵌樣式
element.currentStyle //IE瀏覽器獲取非內(nèi)嵌樣式
window.getComputedStyle(element,偽類) //非IE瀏覽器獲取非內(nèi)嵌樣式
document.defaultView.getComputedStyle(element,偽類)//非IE瀏覽器獲取非內(nèi)嵌樣式
注:Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 之前,第二個(gè)參數(shù)“偽類”是必需的(如果不是偽類,設(shè)置為null),現(xiàn)在可以省略這個(gè)參數(shù)。

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

<!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="關(guān)鍵字">
    <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樣式對(duì)象:CSS2Properties{},CSSStyleDeclaration
    console.log(test.style); //火狐返回空對(duì)象CSS2Properties{},谷歌返回空對(duì)象CSSStyleDeclaration{} 
    console.log(tag.style); //返回CSS2Properties{width:"500px",height:"300px",background-color:"pink"}
    //element.style獲取的是內(nèi)嵌式的style,如果不是內(nèi)嵌式,則是一個(gè)空對(duì)象

    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對(duì)象
    console.log(window.getComputedStyle(test,null))//谷歌返回CSSStyleDeclaration{……} ,火狐返回CSS2Properties{……}
    console.log(window.getComputedStyle(test))
    //效果同上,但是在Gecko 2.0 (Firefox 4/Thunderbird 3.3/SeaMonkey 2.1) 之前,第二個(gè)參數(shù)“偽類”是必需的(如果不是偽類,設(shè)置為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>

以上的例子僅是驗(yàn)證前面的論述是否正確。

為了簡(jiǎn)單,我們也可以對(duì)獲取樣式做一個(gè)簡(jiǎn)單的封裝。

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,不過(guò)現(xiàn)在已經(jīng)不必
    console.log(getStyle(test,"stylefloat"));//火狐和谷歌都是undefined
    console.log(getStyle(test,"styleFloat")); //IE9以下必須使用styleFloat,IE9及以上,支持styleFloat和cssFloat

    console.log(window.getComputedStyle(test).getPropertyValue("float"))

對(duì)應(yīng)float樣式,IE中使用的是styleFloat,而早前的FF和chrome使用的是cssFloat,現(xiàn)在FF和Chrome已經(jīng)支持float,還有一些其他的屬性,不再一一列出,為了不去記憶這些差異點(diǎn),我們引出兩個(gè)訪問(wèn)CSS樣式對(duì)象的方法:
getPropertyValue方法和getAttribute方法

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

對(duì)于IE6~8,需要使用getAttribute方法,用于訪問(wèn)CSS樣式對(duì)象的屬性

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,輕松使用JS獲取CSS樣式。

相關(guān)文章

最新評(píng)論