getComputedStyle获取到的是经过浏览器运算后的样式ie6-8不兼容
currentStyle chorme FF不兼容
function getStyle(obj,attr){
/*if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj)[attr];
}*/
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr];
}
getStyle:
如果获取到的是复合样式,改成单一的样式:backgroundColor 不要用来做判断 img 先对src 可以赋值
不要用空格 不然会undefined
不要获取未设置后的样式
本文介绍了一种兼容IE6-8及现代浏览器如Chrome、Firefox的元素样式获取方法。通过自定义getStyle函数实现对currentStyle与getComputedStyle的支持,并提供了一些使用技巧。
157

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



