gpt4 book ai didi

javascript - $(文档) 与 $ ("document")

转载 作者:可可西里 更新时间:2023-11-01 02:30:36 26 4
gpt4 key购买 nike

$(document)$("document") 之间有什么区别吗?

编辑:也当进入 .ready() 例如$("文档").ready()

最佳答案

$(document)使用 jQuery 包装全局 document对象。

$("document")试图寻找 <document>元素,这在 HTML 中显然没有意义,因为没有这样的元素,只有一个根 document标记中由 <html> 表示的对象元素。它的行为方式是因为通过将字符串传递给 jQuery 函数,您实际上是在给它一个 selector。 .

重新编辑:如patrick dw说,在 ready() 的上下文中没有区别,事实上从 jQuery 3.0 开始使用 $(document)完全或明确调用 ready()已弃用。来自documentation :

jQuery offers several ways to attach a function that will run when the DOM is ready. 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; 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. For example, the third syntax works with "document" which selects nothing. The fourth syntax waits for the document to be ready but implies (incorrectly) that it waits for images to become ready.

关于javascript - $(文档) 与 $ ("document"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4785952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com