[英]what's the difference bewteen document.defaultView.getComputedStyle and window.getComputedStyle
when get a element's style, we always use当获取元素的样式时,我们总是使用
if(document.defaultView && document.defaultView.getComputedStyle)
to check whether the browser support the method or not. if(document.defaultView && document.defaultView.getComputedStyle)
检查浏览器是否支持该方法。
why not use if(window.getComputedStyle)
?为什么不使用
if(window.getComputedStyle)
?
So in short, the reason why we use document.defaultView && document.defaultView.getComputedStyle
is that we want a cross-browser working-on-every-element method of checking whenever it supports fetching computed styles.所以简而言之,我们使用
document.defaultView && document.defaultView.getComputedStyle
的原因是我们想要一个跨浏览器的工作在每个元素上的方法来检查它是否支持获取计算样式。
Simple if(window.getComputedStyle)
would fail for iframes in Firefox 3.6 (according to article linked in comment by Alex K.).对于 Firefox 3.6 中的 iframe,简单的
if(window.getComputedStyle)
将失败(根据 Alex K. 评论中链接的文章)。
According to the MDN defaultView
is no longer required根据MDN
defaultView
不再需要
In many code samples, getComputedStyle is used from the document.defaultView object.
在许多代码示例中,从 document.defaultView 对象中使用 getComputedStyle。 In nearly all cases, this is needless, as getComputedStyle exists on the window object as well.
在几乎所有情况下,这是不必要的,因为 getComputedStyle 也存在于 window 对象上。 It's likely the defaultView pattern was a combination of folks not wanting to write a testing spec for window and making an API that was also usable in Java.
很可能 defaultView 模式是人们不想为窗口编写测试规范和制作在 Java 中也可用的 API 的组合。
There was a bug in Firefox 3.6 (2010/2011) that needed defaultView
fix Firefox 3.6 (2010/2011) 中存在一个需要
defaultView
修复的 错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.