我有以下JQuery函数,它接受用户输入并将其显示在屏幕上.当我同时选择$(document)和$(window)时,该功能有效.使用任何一个选择器的缺点是什么?我在哪里可以阅读有关这些选择器及其区别的更多信息?
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?
先谢谢您.
$(document).keypress(function(e) { if(e.keyCode == 13) { var id = $("input#example").val() console.log(id); $('#data').append(id); } });推荐答案
同时在window或document对象rel ="noreferrer"> jQuery dom选择器,大多数时候您不会注意到两者之间的区别.
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-引用视口.它用作JavaScript中的主要全局对象. document-window的直接后代;指文档树的根.
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.
所有DOM元素都是document的后代,而document是window的直接后代.
All DOM elements are a descendant of the document, which is a direct descendant of window.
更多推荐
文档选择器和窗口选择器有什么区别?
发布评论