开发者

document.onclick vs window.onclick

开发者 https://www.devze.com 2022-12-21 20:14 出处:网络
Is there any difference between document.onclick and window.on开发者_如何转开发click event? Thanks.The JavaScript Window object is the highest level JavaScript object which corresponds to the

Is there any difference between

document.onclick

and

window.on开发者_如何转开发click

event?

Thanks.


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

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.

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.onclick is the preferred choice.


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


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)

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/)

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号