原生js获取元素的样式

根据 MDN介绍:

window.getComputedStyle(elem,null).getPropertyValue("background-color");在ie11下测试不可用,非ie可以,也可以简化:window.getComputedStyle(overlay)["background-color"]效果同上:为了兼容ie:

  1. function getStyle(element, att){
  2.     //特性侦测
  3.     if(window.getComputedStyle){
  4.         //优先使用W3C规范
  5.         return window.getComputedStyle(element)[att];
  6.     }else{
  7.         //针对IE兼容
  8.         return element.currentStyle[att];
  9.     }
  10. }

测试元素的background-color为 rgba(0, 0, 0, 0) ,ie9及其他浏览器没有任何问题,ie8返回的是transparent 

posted @ 2018-06-28 09:30  smartwange  阅读(24)  评论(0)    收藏  举报