currentStyle与getComputedStyle

1.getComputedStyle()    获取到的是计算机(浏览器)计算后的样式

ie9以下不兼容//获取元素的宽度

oDiv.style.cssText="350px;";
alert(getComputedStyle(oDiv).width);//350

//解决兼容性问题
alert(oDiv.currentStyle.width);    //标准浏览器不兼容


//总结
if(oDiv.currentStyle){
  alert(oDiv.currentStyle.width);
}else{
  alert(getComputedStyle(oDiv).width);
}



//封装成方法
function getStyle(obj,attr){
   if(obj.currentStyle){

    return obj.currentStyle[attr];
  }else{
    return getComputedStyle(obj)[attr];
  }
}

getStyle("oDiv","width");

  

  

原文地址:https://www.cnblogs.com/yangxue72/p/7808253.html