JS通过兼容性写法获取样式
获取非行内样式可以使用以下方式:
一 IE中使用box.currentStyle.width
二 非IE浏览器中使用getComputedStyle(box,null).width
一 IE中使用box.currentStyle.width
二 非IE浏览器中使用getComputedStyle(box,null).width
// 兼容处理
if (box.currentStyle) {
box.currentStyle.width;
}else{
getComputedStyle(box,false).width;
}
function getStyle(obj,sty){
if (obj.currentStyle) {
return obj.currentStyle[sty];
}else{
return getComputedStyle(obj,false)[sty];
}
}
console.log(getStyle(box,"height"));
本文介绍了一种在JavaScript中获取元素样式的兼容性方法。针对IE和其他非IE浏览器,提供了不同实现方式,并封装了一个通用函数getStyle来统一处理样式获取过程。
187

被折叠的 条评论
为什么被折叠?



