getComputedStyle与currentStyle获取样式(style/class)

用document.getElementById(‘element').style.xxx获取行内样式

window.getComputedStyle(element,null).color返回的是一个样式对象,有很多值。可以获取外联CSS样式和行内样式,但是它不兼容IE,而且是只读的,不能设置样式

element.currentStyle.color是IE的写法

所以可以写成:

return window.getComputedStyle ? window.getComputedStyle(obj,null).color : obj.currentStyle.color; 

color它返回的是rgb(255,255,255) 

jquery的css()方法就是用了document.defaultView.getComputedStyle相当于window.getComputedStyle

写成这样是为了兼容FF :当使用Firefox 3.6时,其frame中需要使用document.defaultView去获取window对象,才能使用其getComputedStyle方法

原文地址:https://www.cnblogs.com/t1amo/p/6755227.html