JS获取dom的CSS样式

获取样式

//获取样式属性值
function getStyle(obj, attr) {
    if(obj.currentStyle) {    //兼容IE
      return obj.currentStyle[attr];    
    } else {    //兼容火狐谷歌
      return window.getComputedStyle(obj,false)[attr];
    }
}

获取属性一般用的是ele.style.border这种形式的方法, 但是这种方法是有局限性的。该方法只能获取到行内样式,获取不了外部的样式。

currentStyle:该属性只兼容IE,不兼容火狐和谷歌

 写法:ele.currentStyle["attr"]或者ele.currentStyle.attr;

getComputedStyle:该属性是兼容火狐谷歌,不兼容IE

写法:window.getComputedStyle(ele,false)[attr]获取是window.getComputedStyle(ele,false).attr


猜你喜欢

转载自blog.csdn.net/qq_37746973/article/details/80750180
今日推荐