GetStyle By CSS

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   使用   sp   

  我們知道通過

    document.getElementById("argument"); //argument(元素);

    value = argument.style.width;的方式可以輕鬆擷取  或者讀寫該argument的樣式(寬度)。但是我們知道這個屬性是在<HMTL>標籤中的,那麼如果我們argument的樣式寫在CSS中呢?我們就需要用別的方法來擷取它的樣式值.那麼,我們就要用到

    getComputedStyle("argument",":after") .width  //Google、Firefox等非IE核心瀏覽器

    argumet.currentStyle.width  //IE瀏覽器擷取樣式

    這兩種方法來擷取CSS檔案中的樣式值。那麼我們可以通過下面的方法來判別瀏覽器以及擷取並return出我們要的值。

 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 <title>GetStyle</title> 7 <meta name="description" content=""> 8 <meta name="keywords" content=""> 9 <link href="" rel="stylesheet">10 <style>11     .box{12         width: 200px;13         background: gray;14     }15 </style>16 </head>17 <body>18     <div class="box" id="box" style="height:200px">19         20     </div>21 </body>22 <script>23     var obox = document.getElementById("box");24     function getStyle (ele,prop) {25         var value;26         if(prop in document.body.style){27             if(window.getComputedStyle){28                 value = getComputedStyle(ele,null)[prop];29             }else{30                 value = obox.currentStyle.width;31             }    32         }else{33             value = ele.style[prop];34         }35         value = parseFloat(value);36         return value;37     }38     alert(getStyle(obox,"width"));39     alert(getStyle(obox,"height"));40 </script>41 </html>

    經過網上查詢(原文地址:http://blog.163.com/[email protected]/blog/static/180469812013217101436660/)

      瞭解到getComputedStyle與style的區別

        1、唯讀與可寫        getComputedStyle方法是唯讀,只能擷取樣式,不能設定;而element.style能讀能寫。        2、擷取的物件範圍      getComputedStyle方法擷取的是最終應用在元素上的所有CSS屬性對象(即使沒有CSS代碼,也會把預設的祖宗八代都顯示出來);而  element.style只能擷取元素style屬性中的Style樣式。因此對於一個光禿禿的元素<p>,getComputedStyle方法返回對象中length屬性值(如果有)就是190+(據我測試FF:192, IE9:195, Chrome:253, 不同環境結果可能有差異), 而element.style就是0。            getComputedStyle與currentStyle      currentStyle是IE瀏覽器自己的一個屬性,其與element.style可以說是近親,至少在使用形式上類似,element.currentStyle,差別在於element.currentStyle返回的是元素當前應用的最終CSS屬性值(包括外鏈CSS檔案,頁面中嵌入的<style>屬性等)。因此,從作用上將,getComputedStyle方法與currentStyle屬性走的很近,形式上則style與currentStyle走的近。不過,currentStyle屬性貌似不支援偽類樣式擷取,這是與getComputedStyle方法的差異,也是jQuery css()方法無法體現的一點。            getPropertyValue方法      getPropertyValue方法可以擷取CSS樣式申明對象上的屬性值(直接屬性名稱),例如:        window.getComputedStyle(element, null).getPropertyValue("float");      用getPropertyValue方法不必可以駝峰書寫形式(不支援駝峰寫法),例如:style.getPropertyValue("border-top-left-radius");      相容性:getPropertyValue方法IE9+以及其他現代瀏覽器都支援,對於IE6-8有另外一套方案

GetStyle By 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.