js擷取css樣式方法

來源:互聯網
上載者:User

標籤:ntb   pre   css   styles   連結   行間樣式   str   class   code   

一、CSS樣式共有三種:內聯樣式(行間樣式)、內部樣式、外部樣式(連結式和匯入式)

<div id="a" style="width: 100px;height: 100px;"></div>
<style type="text/css">    #a{        width: 100px;        height: 100px;    }</style><body>    <div id="a"></div></body>
<!-- 外部CSS樣式 --><!-- 連結式 --><link rel="stylesheet" type="text/css" href="css/temp.css"/><style type="text/css">    <!-- 匯入式 -->    @import url("css/style.css");</style>

優先順序:一般情況下:內聯樣式  >  內部樣式  >  外部樣式
特殊情況下:當外部樣式放在內部樣式之後,外部樣式將覆蓋內部樣式。

<style type="text/css">    #a{        width: 200px;        height: 200px;        background-color: red;    }</style><link rel="stylesheet" type="text/css" href="css/temp.css"/>

二、js擷取css樣式

1、內聯樣式(行間樣式)的擷取

<div id="a" style="width: 100px;height: 100px;background-color: blue;"></div>
function temp(){    var a=document.getElementById("a");    var aColor=a.style.backgroundColor;    var aWidth=a.style["width"];    alert(aColor+"  "+aWidth);//    rgb(0,0,255)  100px}

2、內部樣式的擷取

<style type="text/css">    #a{        width: 200px;        height: 200px;        background-color: red;    }</style><body>    <div id="a"></div></body>
function temp(){//  非IE瀏覽器    var a=document.getElementById("a");    var aStyle=getComputedStyle(a);    var aColor=aStyle.backgroundColor;    var aWidth=aStyle["width"];    alert(aColor+"  "+aWidth);//  rgb(255,0,0)  200px//  IE瀏覽器//  var a=document.getElementById("a");//  var aStyle=a.currentStyle;//  var aColor=aStyle.backgroundColor;//  var aWidth=aStyle["width"];//  alert(aColor+"  "+aWidth);//  red  200px}

3、外部樣式的擷取(同內部樣式)

<!-- css檔案 -->#a{    width: 300px;    height: 300px;    background-color: #4F5F6F;}

 

js擷取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.