我们都知道,DOM2级的 document.defaulyView.getComputedStyle(Div,null) 方法可以获得元素计算后的样式,但该方法与直接用 window.getComputedStyle(Div,null) 的区别是什么呢?
我在做学习的过程中发现,这两种使用方法对 W3C 的浏览器没有任何影响,虽然 IE 浏览器不支持该方法,但是,当我在js文件中使用下面的程序:
if (typeof document.defaultView.getComputedStyle != "undefined") {
computedStyle=document.defaultView.getComputedStyle(elements,null);
return computedStyle[attr];
} else if (typeof elements.currentStyle != "undefined") {
return elements.currentStyle[attr];
}
当使用不频繁时,低版本的 IE 浏览器会报错,但不影响程序的执行效果;但当频繁使用到该程序时,低、高版本的 IE 都会报错,且不能正常工作。
现在还是没有搞清楚原因,留待后续解决!
而使用 window..getComputedStyle() 就不会出现上面的问题。