简体   繁体   English

document.defaultView有什么意义?

[英]What's the point of document.defaultView?

What's the point of document.defaultView ? document.defaultView什么意义?

MDN says : MDN说

In browsers returns the window object associated with the document or null if none available. 在浏览器中返回与文档关联的窗口对象,如果没有,则返回null。

Code like the following (from PPK's site ) makes use of document.defaultView : 像以下代码(来自PPK的网站 )使用document.defaultView

function getStyle(el,styleProp)
{
    var x = document.getElementById(el);
    if (x.currentStyle)
        var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
    return y;
}

Code like this can be found in other places, like David Mark's My Library . 像这样的代码可以在其他地方找到,比如David Mark的My Library I'm not sure if people are just copying from PPK or some other source or coming up with this independently, but I don't understand it. 我不确定人们是否只是从PPK或其他来源复制或独立提出,但我不明白。

My question is, what is the point of using document.defaultView in cases like this? 我的问题是,在这种情况下使用document.defaultView有什么意义? Wouldn't it be easier to write this as follows: 写这个不是更容易如下:

function getStyle(element, styleProp) {
    if (element === ''+element) element = document.getElementById(element);
    return element.currentStyle ? element.currentStyle[styleProp] :
           getComputedStyle(x,null).getPropertyValue(styleProp);
}

What does document.defaultView.getComputedStyle do that window.getComputedStyle or simply getComputedStyle does not? document.defaultView.getComputedStyle做了什么window.getComputedStyle或者只是getComputedStyle没有?


cwolves' answer got me thinking in the right direction. cwolves的回答让我思考正确的方向。 The original function is silly, missing the point of defaultView . 原来的功能很傻,缺少defaultView的观点。 My proposal above is less silly, but also missing the point of defaultView . 我上面提出的建议不那么愚蠢,但也缺少defaultView的观点。 Here's my new proposal: 这是我的新提案:

function getStyle(element, styleProp) {
    var view = element.ownerDocument && element.ownerDocument.defaultView ?
                element.ownerDocument.defaultView : window;

    return view.getComputedStyle ? 
                view.getComputedStyle(element,null).getPropertyValue(styleProp) : 
            element.currentStyle ? 
                element.currentStyle[styleProp] : null;
}

The element itself must be passed in, not the id. 必须传入元素本身,而不是id。 I think this is probably to be preferred anyway. 我认为这可能是首选。 This gets the document containing the node, and the window associated with it. 这将获取包含节点的文档以及与之关联的窗口。 It has a fallback to the current window's getComputedStyle if ownerDocument or defaultView are broken (I vaguely remember getComputedStyle being around before defaultView ). 如果ownerDocumentdefaultView被破坏,它会回getComputedStyle当前窗口的getComputedStyle (我getComputedStyle记得getComputedStyledefaultView之前出现)。 This is probably closer to the intended use of defaultView . 这可能更接近defaultView的预期用途。

I'm not positive on this, but I imagine that it's the result of fixing a bug from either trying to run code on a detached document (ie something that exists in memory but is not in the page) or trying to run on a document in a different window (eg an iframe or a popup). 我对此并不乐观,但我认为这是修复一个错误的结果,试图在分离的文档上运行代码(即存在于内存中但不在页面中的内容)或尝试在文档上运行在不同的窗口(例如iframe或弹出窗口)。

According to your quote, when document.defaultView is run on a document that is NOT the current document, you will get the associated window object, thus document.documentView.getComputedStyle !== getComputedStyle since they are in different contexts. 根据您的引用,当document.defaultView在不是当前文档的document.defaultView上运行时,您将获得关联的窗口对象,因此document.documentView.getComputedStyle !== getComputedStyle因为它们位于不同的上下文中。

In short, I believe it's akin to document.window which doesn't exist. 简而言之,我认为它类似于不存在的document.window

The OP asks the question, "What's the point of document.defaultView ", and the answer really doesn't have anything to do with getComputedStyle. OP问了一个问题,“ document.defaultView有什么意义”,答案真的与getComputedStyle没有任何关系。 The document.defaultView property is simply a way of obtaining the window object if one has a reference to the document object contained in that window . document.defaultView属性只是获取window对象的一种方法,如果有一个window对象包含该window包含的document对象。 There are cases where the window object you are seeking to reference (or defaultView ) is not in the same window scope as the code you are running. 在某些情况下,您要引用的window对象(或defaultView )与您运行的代码不在同一窗口范围内。

One example of this is if you have a reference to the document object in an iframe, and wish to conveniently get a reference to the window object of that iframe. 这方面的一个示例是,如果您在iframe中引用了document对象,并希望方便地获取对该iframe的window对象的引用。

Another case might be where you are running in privileged context in the browser scope (eg, chrome code in Firefox), and you happen to have a reference to the document object of a tabbrowser, or another window. 另一种情况可能是您在浏览器范围中的特权上下文中运行的位置(例如,Firefox中的chrome代码),并且您碰巧引用了tabbrowser或其他窗口的document对象。

Or, as Dagg Nabbit points out, if in any of these cases you have a reference to an element within the window, you could access the parent window of that element through element.ownerDocument.defaultView 或者,正如Dagg Nabbit指出的那样,如果在任何这些情况下你有一个对窗口内元素的引用,你可以通过element.ownerDocument.defaultView访问该元素的父window element.ownerDocument.defaultView

It is simply an abstraction as far as I can tell, just in case any user agents pop up with a DOM implementation, but don't provide a view in the form of a window. 就我所知,它只是一个抽象,以防任何用户代理弹出DOM实现,但不提供窗口形式的视图。 See Views in DOM level 2. 请参阅DOM级别2中的视图

According to MDN getComputedStyle article , 根据MDN getComputedStyle文章

In many code samples online, getComputedStyle is used from the document.defaultView object. 在许多在线代码示例中, getComputedStyle用于document.defaultView对象。

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 some combination of 它可能是defaultView模式的某种组合

  1. folks not wanting to write a spec for window and 人们不想为窗口编写规范
  2. making an API that was also usable in Java. 制作一个也可以在Java中使用的API。

However, there is a single case where the defaultView 's method must be used: when using Firefox 3.6 to access framed styles. 但是,有一种情况是必须使用defaultView的方法:使用Firefox 3.6访问框架样式时。

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

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