JS擷取元素計算過後的樣式

來源:互聯網
上載者:User

標籤:沒有   mil   com   文法   css   支援   let   返回   soft   

擷取元素計算過後的樣式

Window.getComputedStyle()

方法會在一個元素應用完有效樣式且計算完所有屬性的基本值之後給出所有 CSS 屬性的值。

文法: let style = window.getComputedStyle(element, [pseudoElt]); 

element:用於擷取計算養殖的element

pseudoElt:可選,指定一個更匹配的虛擬元素的字串,必須對普通元素省略或null

傳回值:是指指定元素或虛擬元素的所有屬性的集合

如果沒有虛擬元素,,虛擬元素這個參數是可以省略的,或者寫null,但是為了相容,null不要省略

瀏覽器安全色:

IE8不支援

需要使用currentStyle擷取當前樣式的任意屬性值

element.currentStyle

在IE低版本可用。

 

相容寫法

  //相容的擷取當前樣式的任意屬性值    function getStyle(element) {        if(window.getComputedStyle){            return window.getComputedStyle(element,null);        }else {            return element.currentStyle;        }    }    //擷取對應的屬性的值   function getStyle_2(element,attr) {       if(window.getComputedStyle){           return window.getComputedStyle(element,null)[attr];       }else {           return element.currentStyle[attr];       }   }

 

 

 

JS擷取元素計算過後的樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.