Javascript document.onclick 与 window.onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2384127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:02:12  来源:igfitidea点击:

document.onclick vs window.onclick

javascriptjavascript-events

提问by Nazmul

Is there any difference between

有什么区别吗

document.onclick

and

window.onclick

event?

事件?

Thanks.

谢谢。

回答by Justin Ethier

The JavaScript Window object is the highest level JavaScript object which corresponds to the web browser window.

JavaScript Window 对象是最高级别的 JavaScript 对象,对应于 Web 浏览器窗口。

The document object is the container for all HTML HEAD and BODY objects associated within the HTML tags of an HTML document. This could correspond to the top-most window, or an iframe within the window.

文档对象是 HTML 文档的 HTML 标签内关联的所有 HTML HEAD 和 BODY 对象的容器。这可以对应于最顶部的窗口,或窗口内的 iframe。

Update

更新

After a quick test there really is no difference between the two. However, as others have said, window.onclick did not work when tested in IE8. So apparently the bottom line is that document.onclickis the preferred choice.

经过快速测试,两者之间确实没有区别。但是,正如其他人所说,在 IE8 中测试时 window.onclick 不起作用。显然,底线是这document.onclick是首选。

回答by KTastrophy

I've heard of some versions of IE not supporting window.onclick

我听说有些版本的 IE 不支持 window.onclick

回答by BenMills

The w3c describes the document as: "The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data." (http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document)

w3c 将文档描述为:“文档接口代表整个 HTML 或 XML 文档。从概念上讲,它是文档树的根,并提供对文档数据的主要访问。” ( http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document)

and window as: "...defines the Window object, which provides the global namespace for web scripting languages, access to other documents in a compound document by reference, navigation to other locations, and timers. The Window object is a long-standing de facto standard for HTML user agents. However, it should not be assumed based on this or the name "Window" that it is limited to HTML or to visual user agents." (http://www.w3.org/TR/Window/)

和 window as: "...定义了 Window 对象,它为 web 脚本语言提供全局命名空间、通过引用访问复合文档中的其他文档、导航到其他位置和计时器。Window 对象是一个长期存在的HTML 用户代理的事实上的标准。但是,不应基于此或名称“窗口”假定它仅限于 HTML 或可视用户代理。” ( http://www.w3.org/TR/Window/)

So to answer your question, depending on the browser there might not be any issue. But some browser vendors might implement them differently.

因此,要回答您的问题,根据浏览器的不同,可能没有任何问题。但是一些浏览器供应商可能会以不同的方式实现它们。

回答by Anthony Forloney

If there are any differences at all, I am not aware of any. I believe in the end the documentreferences the window.

如果有任何差异,我不知道。我相信最终document引用了window.