高级浏览器: chrome firefox ie9及以上符合web标准浏览器。
低版本浏览器: ie6,ie7,ie8。
getComputedStyle和currentStyle二者的适用范围:
getComputedStyle只能高级浏览器使用,ie低版本不能使用。
currentStyle ie浏览器都能使用。
解决方案:高级浏览器用getComputedStyle,ie低版本currentStyle。
由于getComputedStyle只能高级浏览器使用,所以说在高级浏览器的window对象下有getComputedStyle的方法。在ie低版本中使用getComputedStyle,会报错,所以window.getComputedStyle作为区分要点,因为在高级浏览器中有这个属性,在低版本浏览器中相当于在window下添加了一个getComputedStyle属性,没有赋值,为undefined。
参数1:所要读取样式的标签名
参数2:所要读取的css样式名
function getStyle(obj,pro){
if(window.getComputedStyle != undefined){
return getComputedStyle(obj)[pro];
}else{
return obj.currentStyle[pro];
}
}
调用函数:getStyle(box,‘width’);
js 兼容性封装读取css样式的函数:
最新推荐文章于 2021-04-27 21:12:35 发布