繁体   English   中英

document.defaultView.getComputedStyle 和 window.getComputedStyle 有什么区别

[英]what's the difference bewteen document.defaultView.getComputedStyle and window.getComputedStyle

当获取元素的样式时,我们总是使用

if(document.defaultView && document.defaultView.getComputedStyle)检查浏览器是否支持该方法。

为什么不使用if(window.getComputedStyle)

所以简而言之,我们使用document.defaultView && document.defaultView.getComputedStyle的原因是我们想要一个跨浏览器的工作在每个元素上的方法来检查它是否支持获取计算样式。

对于 Firefox 3.6 中的 iframe,简单的if(window.getComputedStyle)将失败(根据 Alex K. 评论中链接的文章)。

根据MDN defaultView不再需要

在许多代码示例中,从 document.defaultView 对象中使用 getComputedStyle。 在几乎所有情况下,这是不必要的,因为 getComputedStyle 也存在于 window 对象上。 很可能 defaultView 模式是人们不想为窗口编写测试规范和制作在 Java 中也可用的 API 的组合。

Firefox 3.6 (2010/2011) 中存在一个需要defaultView修复的 错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM