文档选择器和窗口选择器有什么区别?

发布于 2024-11-10 02:43:31 字数 363 浏览 2 评论 0原文

我有以下 JQuery 函数,它接受用户输入并将其显示在屏幕上。当我选择 $(document)$(window) 时,该函数起作用。使用任一选择器有什么缺点?在哪里可以阅读有关这些选择器及其差异的更多信息?

先感谢您。

  $(document).keypress(function(e) {
      if(e.keyCode == 13) {
          var id = $("input#example").val()
          console.log(id);
          $('#data').append(id);
      }
  });

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?

Thank you in advance.

  $(document).keypress(function(e) {
      if(e.keyCode == 13) {
          var id = $("input#example").val()
          console.log(id);
          $('#data').append(id);
      }
  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁憨憨 2024-11-17 02:43:31

$(window) 选择器用于选择视口

$(document) 选择器用于整个文档(即 内的内容) code> 标记,即使它超出了视口)。

$(window) selector is for selecting the viewport

$(document) selector is for the entire document (that is, what's inside the <html> tag, even if it exapnds beyond the viewport).

音盲 2024-11-17 02:43:31

jQuery 中使用 windowdocument 对象时dom 选择器,大多数时候你不会注意到两者之间的区别。

然而,重要的是要注意它们不是同一个对象。

window - 指视口。它用作 JavaScript 中的主要全局对象。
document - window 的直接后代;指的是 文档树的根

所有 DOM 元素都是 document 的后代,而 documentwindow 的直接后代。

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.

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.

All DOM elements are a descendant of the document, which is a direct descendant of window.

梦醒时光 2024-11-17 02:43:31

为了回答这个问题,让我从 DOM 的定义开始,即我们通常所说的“文档”。

文档对象模型 (DOM) 是用于有效 HTML 和格式良好的 XML 文档的应用程序编程接口 (API)。它定义了文档的逻辑结构以及访问和操作文档的方式。在 DOM 规范中,术语“文档”是广义使用的。

现在让我解释一下我发现的关于浏览上下文的内容,因为这是文档和窗口通常具有的关系 -尽管值得一提的是,文档可能在没有浏览上下文的情况下存在,但您永远不应该在 jquery 中看到这种情况。

用户与文档的主视图进行交互。视图被定义为用于向用户代理呈现文档的媒体,例如屏幕、打印、语音。主视图是默认视图,由实现 Window 接口的 AbstractView 对象表示。

简而言之,window 是容器,document 是内容。但我确实建议至少浏览一下相关文档,以便更好地理解。

来源:

To answer this question let me begin with the definition of the DOM, what we commonly know as "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.

Now let me explain a little of what I found about browsing contexts, 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.

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.

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.

Sources:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文