简体   繁体   English

javascript / Jquery中的$(function(){}表示什么

[英]$(function () { } in javascript/Jquery means what

Please let me know .What this below statment means in JS- 请让我知道。以下陈述在JS-中是什么意思

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

Is this a anonymous function/ or a Jquery equivalent to document.ready? 这是一个匿名函数/或等效于document.ready的Jquery吗?

Thank You, 谢谢,

there is no diff between 之间没有区别

$(function () { });

and

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

Both are use for wrapping when dom is ready. dom准备就绪时,两者都用于包装。

actually $() is shorthand for $( document ).ready() 实际上$()$( document ).ready()简写

Breaking it down: 分解:

$ is an alias of jQuery - the global function defined by the jQuery library. $jQuery的别名-jQuery库定义的全局函数。

This statement makes a call to the $ function: 该语句调用$函数:

$(/* args */)

This function accepts various different types of arguments, and behaves differently according to what argument(s) you pass it. 此函数接受各种不同类型的参数,并且根据您传递的参数的不同而表现不同。

In the statement in question, an anonymous function is being passed as the single argument to the $ function: (note that a closing parenthesis is required to complete the statement originally given in the question): 在所讨论的语句中,匿名函数作为单个参数传递给$函数:(请注意,需要使用圆括号来完成问题中最初给出的语句):

$(function () { /* Code */ })

If passed a function , $ will add the function as an event handler for jQuery's [DOM] ready event. 如果传递了一个function ,则$将把该函数添加为jQuery的[DOM] ready事件的事件处理程序。 This means that the function will be queued up to be executed when the document has finished loading. 这意味着该功能将在文档加载完成时排队等待执行。 If the document has already finished loading, the function will simply be executed immediately. 如果文档已经完成加载,则该函数将立即执行。

In this way, passing a function to $ acts as a shorthand version of: 这样,将函数传递给$可以作为以下内容的简写形式:

$(document).ready(function() {
    /* code to execute on dom ready */
})

It's both an anonymous function (you created a function without giving it name) and a shorthand for the document ready event handler . 它既是一个匿名函数(您在未指定函数名称的情况下创建了一个函数),又是文档就绪事件处理程序简写形式

Note that you are also missing a closing bracket in your code, it should be 请注意,您还缺少代码中的右括号,它应该是

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

$(function () { }); is executed as soon as the DOM is parsed and is invoked in order of appearance if there are multiple appearances. 在DOM解析后立即执行,并在出现多个外观时按出现顺序调用。 At this point the document is however not displayed, its just parsed. 但是,此时文档尚未显示,仅被解析。

and is the equivalent of $(document).ready(function () { }); 相当于$(document).ready(function () { });

The obvious benefit is that having your script tag before other elements on the page means that your script can interact with them even though they're not available at parse time. 明显的好处是,在页面上的其他元素之前放置脚本标签意味着您的脚本可以与它们进行交互,即使它们在解析时不可用。 If you run your script before elements have been parsed and the document is not ready, they will not be available for interaction. 如果在元素被解析且文档尚未准备好之前运行脚本,则它们将不可用于交互。

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

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