jQuery 文档选择器和窗口选择器有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6143373/
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
What is the difference between the document selector and window selector?
提问by JZ.
I have the following JQuery function that takes user input and displays it on screen. When I select for both $(document)
and $(window)
the function works. What is the disadvantage to using either selector? Where can I read more on these selectors and their differences?
我有以下 JQuery 函数,它接受用户输入并将其显示在屏幕上。当我选择两者$(document)
并且$(window)
功能有效时。使用任一选择器的缺点是什么?我在哪里可以阅读有关这些选择器及其差异的更多信息?
Thank you in advance.
先感谢您。
$(document).keypress(function(e) {
if(e.keyCode == 13) {
var id = $("input#example").val()
console.log(id);
$('#data').append(id);
}
});
采纳答案by Shaz
While using the window
or document
object in a jQuery dom selector, most of the time you won't notice a difference between the two.
在jQuery dom 选择器中使用window
ordocument
对象时,大多数情况下您不会注意到两者之间的区别。
However, it's important to note that they are not the same object.
但是,重要的是要注意它们不是同一个对象。
window
- refers to the viewport. It's used as the main global object in JavaScript. document
- a direct descendant of window
; refers to the root of the document tree.
window
- 指的是视口。它在 JavaScript 中用作主要的全局对象。document
- 的直系后代window
;指的是文档树的根。
All DOM elements are a descendant of the document
, which is a direct descendant of window
.
所有 DOM 元素都是 的后代document
,而 是 的直接后代window
。
回答by Itai Sagi
$(window)
selector is for selecting the viewport
$(window)
选择器用于选择视口
$(document)
selector is for the entire document (that is, what's inside the <html>
tag, even if it exapnds beyond the viewport).
$(document)
选择器用于整个文档(即<html>
标签内的内容,即使它扩展到视口之外)。
回答by Edd
To answer this question let me begin with the definition of the DOM, what we commonly know as "document
".
为了回答这个问题,让我从 DOM 的定义开始,我们通常称之为“ document
”。
The Document Object Model (DOM) is an application programming interface (API) for valid HTML and well-formed XML documents. It defines the logical structure of documents and the way a
document
is accessed and manipulated. In the DOM specification, the term "document" is used in the broad sense.
文档对象模型 (DOM) 是用于有效 HTML 和格式良好的 XML 文档的应用程序编程接口 (API)。它定义了文档的逻辑结构以及
document
访问和操作a 的方式。在 DOM 规范中,术语“文档”是广义的。
Now let me explain a little of what I found about browsing context
s, as that is the relationship that a Document
and a Window
normally have—although it is important to mention that a Document
may exist without a browsing context
, but you should never see that with jquery.
现在让我解释一下我发现的关于browsing context
s 的一些内容,因为这是 aDocument
和 aWindow
通常具有的关系——尽管重要的是要提到 aDocument
可能存在而没有 a browsing context
,但您永远不应该在 jquery 中看到这种关系。
A user interacts with the main view of the Document
. A view is defined as the media that is being used to present the Document
to the user agent—e.g. screen, print, speech. The main view is the default view and is represented by an AbstractView
object that implements the Window
interface.
用户与 的主视图进行交互Document
。视图被定义为用于向Document
用户代理呈现的媒体——例如屏幕、印刷品、语音。主视图是默认视图,由AbstractView
实现Window
接口的对象表示。
And to put it really simple, window
is the container and document
is the content. But I do recommend to at least skim through the documentation of this to have a better understanding.
说得非常简单,window
就是容器,document
就是内容。但我建议至少浏览一下文档以更好地理解。
Sources:
资料来源: