alert( getComputedStyle( $('div1') ).width ); // IE6 7 8 不兼容
alert( $('div1').currentStyle.width ); // 标准浏览器不兼容
总结:
1 .获取到的是计算机(浏览器)计算后的样式
2. background: url() red …… 复合样式(不要获取)
backgroundColor 单一样式(颜色值不要用来做判断)
3. 不要有空格
4. 不要获取未设置后的样式:不兼容
函数封装:
function $( v ){
if( typeof v === 'function' ){
window.onload = v;
} else if ( typeof v === 'string' ) {
return document.getElementById(v);
} else if ( typeof v === 'object' ) {
return v;
}
}
function getStyle( obj, attr ){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle( obj )[attr];
}