js如何擷取樣式?

來源:互聯網
上載者:User

標籤:put   attr   ack   idt   type   round   nload   back   內聯   

     在某個項目中,我們經常會需要來擷取某個元素的樣式,比如說擷取一個div的color;這樣,新的問出現了,

    var style = box.style.width;console.log(style);如果我們寫的是行內的樣式,可以輕鬆通過這種方式來獲得所需要的樣式,不過,如果我們的樣式是內聯或者外聯樣式,就取不到了;

 

<script type="text/javascript">
window.onload=function(){
//擷取行間樣式
var oDiv=document.getElementById("Div");
alert(oDiv.style.background);

//擷取非行間樣式 方法,currentStyle是IE中的屬性,getComputedStyle是其他瀏覽器的方法(注意其有兩個參數);在非行間樣式中,style屬性只能去賦值,在行間樣式中才能去擷取樣式;
function getStyle(obj, attr)
{
if(obj.currentStyle)
{
return obj.currentStyle[attr];
}
else
{
return window.getComputedStyle(obj,false)[attr];
}
}
alert(getStyle(oDiv2,"background")) ;
}

</script>

 

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.