This question already has an answer here:
这个问题在这里已有答案:
- What is the meaning of “$” sign in JavaScript 7 answers
JavaScript 7答案中“$”符号的含义是什么?
I have seen a syntax where one puts a function inside parentheses which follow a dollar sign like this:
我已经看到了一种语法,其中一个函数放在括号中,这些函数遵循如下美元符号:
$(function(){...});
What does this mean in jQuery? What does the function do?
这在jQuery中意味着什么?这个功能有什么作用?
2 个解决方案
#1
28
$(function(){...})
is a shortcut for
$(function(){...})是一个快捷方式
$(document).ready(function(){...});
See the API docs
请参阅API文档
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
$()。ready(处理程序)(不建议这样做)
#2
2
The function inside the parentheses is executed when the DOM is fully loaded.
当DOM完全加载时,括号内的函数被执行。
This is implemented by .ready()
, i. e. as Mohammad Adil already said, it's a shortcut.
这是由.ready(),i实现的。即正如*阿迪尔所说,这是一条捷径。
Excerpt from the documentation for .ready()
:
摘自.ready()的文档:
While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to
.ready()
is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.虽然JavaScript在呈现页面时提供了用于执行代码的加载事件,但在完全接收到所有资产(如图像)之前,不会触发此事件。在大多数情况下,只要完全构造DOM层次结构,就可以运行脚本。传递给.ready()的处理程序保证在DOM准备好后执行,因此这通常是附加所有其他事件处理程序并运行其他jQuery代码的最佳位置。
#1
28
$(function(){...})
is a shortcut for
$(function(){...})是一个快捷方式
$(document).ready(function(){...});
See the API docs
请参阅API文档
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
$()。ready(处理程序)(不建议这样做)
#2
2
The function inside the parentheses is executed when the DOM is fully loaded.
当DOM完全加载时,括号内的函数被执行。
This is implemented by .ready()
, i. e. as Mohammad Adil already said, it's a shortcut.
这是由.ready(),i实现的。即正如*阿迪尔所说,这是一条捷径。
Excerpt from the documentation for .ready()
:
摘自.ready()的文档:
While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to
.ready()
is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.虽然JavaScript在呈现页面时提供了用于执行代码的加载事件,但在完全接收到所有资产(如图像)之前,不会触发此事件。在大多数情况下,只要完全构造DOM层次结构,就可以运行脚本。传递给.ready()的处理程序保证在DOM准备好后执行,因此这通常是附加所有其他事件处理程序并运行其他jQuery代码的最佳位置。