简体   繁体   English

$(文件)与$(“文件”)

[英]$(document) vs. $(“document”)

Is there any difference between: $(document) and $("document") ? $(document)$("document")之间有什么区别吗?

EDIT: also when into the .ready() eg $("document").ready() 编辑:也进入.ready()例如$("document").ready()

$(document) uses jQuery to wrap the global document object. $(document)使用jQuery来包装全局document对象。

$("document") attempts to look for a <document> element, which obviously makes no sense in HTML because there's no such element, only a root document object represented in the markup by the <html> element. $("document")试图寻找一个<document>元素,这在HTML中显然没有意义,因为没有这样的元素,只有<html>元素在标记中表示的根document对象。 It behaves that way because by passing the jQuery function a string, you're actually giving it a selector . 它的行为方式是因为通过传递jQuery函数一个字符串,你实际上给它一个选择器

Re edit: as patrick dw says, in the context of ready() there's no difference, and in fact as of jQuery 3.0 using $(document) at all or explicitly calling ready() is deprecated. 重新编辑:正如patrick dw所说,在ready()的上下文中没有区别,实际上从jQuery 3.0开始使用$(document)或显式调用ready()已被弃用。 From the documentation : 文档

jQuery offers several ways to attach a function that will run when the DOM is ready. jQuery提供了几种附加在DOM准备就绪时运行的函数的方法。 All of the following syntaxes are equivalent: 以下所有语法都是等效的:

  • $( handler )
  • $( document ).ready( handler )
  • $( "document" ).ready( handler )
  • $( "img" ).ready( handler )
  • $().ready( handler )

As of jQuery 3.0, only the first syntax is recommended; 从jQuery 3.0开始,建议只使用第一种语法; the other syntaxes still work but are deprecated. 其他语法仍然有效但不推荐使用。 This is because the selection has no bearing on the behavior of the .ready() method, which is inefficient and can lead to incorrect assumptions about the method's behavior. 这是因为选择与.ready()方法的行为没有关系,这是低效的,并且可能导致对方法行为的错误假设。 For example, the third syntax works with "document" which selects nothing. 例如,第三种语法适用于"document" ,它不选择任何内容。 The fourth syntax waits for the document to be ready but implies (incorrectly) that it waits for images to become ready. 第四种语法等待文档准备就绪,但暗示(错误地)它等待图像准备就绪。

If you're talking about calling the .ready() function, it (currently) makes no difference. 如果您正在谈论调用.ready()函数,它(当前)没有任何区别。

In both cases the argument is ignored. 在这两种情况下,参数都会被忽略。

You could do this: 你可以这样做:

$( "cheese pizza" ).ready(function() {});

To be clear, it is best to use the officially supported calls to .ready() , which are: 需要明确的是,最好使用官方支持的.ready()调用,它们是:

$(document).ready(function(){/*...*/});

and: 和:

$(function(){/*...*/});

When actually selecting the document , you should use $(document) . 实际选择document ,您应该使用$(document)

Your first example wil search for an variable or object called document within your JS. 您的第一个示例将在JS中搜索名为document的变量或对象。 (be carefull, it is a predefined variable) (小心,这是一个预定义的变量)

The second will search the page for a tag with the name "document". 第二个将在页面中搜索名为“document”的标签。

var document = 'div#logo';
$(document); //will search for div#logo
$('document'); //will search for document

document is a pre-made global variable that is the representation of the current HTML document, short for window.document . document是一个预制的全局变量,它是当前HTML文档的表示形式,是window.document缩写。

$("document") , as @BoltClock pointed out, would be using that string as a selector for a <document> element. 正如@BoltClock指出的那样, $("document")将使用该字符串作为<document>元素的选择器。

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

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