通過js獲取外部css樣式


通過js獲取外部樣式表中的屬性,比如.box在style.css樣式表中有個屬性叫font-size:16px;

通過js獲取.box的這個屬性:

js代碼為:

<script>

  window.onload=function(){

       var oBox  = document.getElementsByClassName("box")[0];

  alert(oBox.style.fontSize);//結果返回的是空

       }

</script>

解決方法:

js代碼為:

<script>

  window.onload=function(){

    var oBox  = document.getElementsByClassName("box")[0];

   alert(getElementStyle(oBox,"fontSize"));

}

function getElementStyle(obj,attr){

  if(obj.currentStyle){

    return obj.currentStyle[attr];

  }else{

    return getComputedStyle(obj,false)[attr];

  }

}

</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM