简体   繁体   English

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

[英]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.

相关问题 如何在DOMParser生成的文档中使用getComputedStyle(没有defaultView或window对象)? - How to use getComputedStyle with DOMParser produced document (no defaultView or window objects)? window.getComputedStyle 总是以像素为单位返回测量值 - window.getComputedStyle always returns measurement in pixels 使用 window.getComputedStyle 错误的 css 信息 - Wrong css informations by using window.getComputedStyle 如何修复Window.getComputedStyle不是对象错误 - How to fix Window.getComputedStyle is not an object error Window.getComputedStyle不显示内联样式 - Window.getComputedStyle does not show inline style body.style.backgroundColor和window.getComputedStyle(body).getPropertyValue('background-color')之间的区别 - Difference between body.style.backgroundColor and window.getComputedStyle(body).getPropertyValue('background-color') 如何从window.getComputedStyle()或其他函数返回rgba值? - How to return rgba value from window.getComputedStyle() or other function? Window.getComputedStyle的参数1未实现接口Element - Argument 1 of Window.getComputedStyle does not implement interface Element window.getComputedStyle不适用于除Chrome之外的其他浏览器的速记属性 - window.getComputedStyle not working for shorthand properties in other browsers except Chrome 如何使用 window.getComputedStyle() 获得全高(包括边距)? - How to get full height (including margins) with window.getComputedStyle()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM