简体   繁体   English

document.onclick vs window.onclick

[英]document.onclick vs window.onclick

Is there any difference between 两者之间有什么区别吗?

document.onclick

and

window.onclick

event? 事件?

Thanks. 谢谢。

The JavaScript Window object is the highest level JavaScript object which corresponds to the web browser window. JavaScript Window对象是与Web浏览器窗口对应的最高级JavaScript对象。

The document object is the container for all HTML HEAD and BODY objects associated within the HTML tags of an HTML document. 文档对象是HTML文档的HTML标记内关联的所有HTML HEAD和BODY对象的容器。 This could correspond to the top-most window, or an iframe within the window. 这可能对应于最顶层窗口或窗口内的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. 但是,正如其他人所说,在IE8中测试时,window.onclick不起作用。 So apparently the bottom line is that document.onclick is the preferred choice. 显然,底线是document.onclick是首选。

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

If there are any differences at all, I am not aware of any. 如果有任何差异,我不知道。 I believe in the end the document references the window . 我相信document最后会引用window

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." w3c将文档描述为:“Document接口代表整个HTML或XML文档。从概念上讲,它是文档树的根,并提供对文档数据的主要访问。” ( http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document ) 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." 和window:“...定义Window对象,它提供Web脚本语言的全局命名空间,通过引用访问复合文档中的其他文档,导航到其他位置和定时器.Windows对象是一个长期存在的对象HTML用户代理的事实标准。但是,不应该基于此或名称“Window”假定它仅限于HTML或可视用户代理。“ ( http://www.w3.org/TR/Window/ ) 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. 但是一些浏览器厂商可能会以不同方式实

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

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