js 獲取任意一個元素的任意一個樣式屬性的值


//谷歌,火狐支持
console.log(window.getComputedStyle(my$("dv"),null).left);
//IE8支持
console.log(my$("dv").currentStyle.left);

//獲取任意一個元素的任意一個樣式屬性的值
//element為對象 attr為要獲取那個屬性
function getStyle(element,attr) { //判斷瀏覽器是否支持這個方法 if(window.getComputedStyle){ return window.getComputedStyle(element,null)[attr]; }else{ return element.currentStyle[attr]; } }

 
        

 簡化

function getStyle(element,attr) {
      return window.getComputedStyle? window.getComputedStyle(element,null)[attr]:element.currentStyle[attr];
    }

 


免責聲明!

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



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