Javascript $(function() {} ); 做?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7642442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:01:52  来源:igfitidea点击:

What does $(function() {} ); do?

javascriptjquery

提问by jwchang

Sometimes I make a function and call the function later.

有时我创建一个函数并稍后调用该函数。

Example:

例子:

function example { alert('example'); }
example(); // <-- Then call it later

Somehow, some functions cannot be called. I have to call those functions inside:

不知何故,有些函数不能被调用。我必须在里面调用这些函数:

$(function() { });

What do $(function() {});and (function() { });mean, and what's the difference/purpose of these?

做什么$(function() {});(function() { });意味着,什么是这些区别/目的是什么?

回答by Russ Clarke

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

is just jQuery short-hand for

只是 jQuery 的简写

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

What it's designed to do (amongst other things) is ensure that your function is called once all the DOM elements of the page are ready to be used.

它的设计目的(除其他外)是确保在页面的所有 DOM 元素都准备好使用时调用您的函数。

However, I don't think that's the problem you're having - can you clarify what you mean by 'Somehow, some functions are cannot be called and I have to call those function inside' ? Maybe post some code to show what's not working as expected ?

但是,我认为这不是您遇到的问题-您能否澄清“不知何故,无法调用某些函数而我必须在内部调用这些函数”的意思?也许发布一些代码来显示什么没有按预期工作?

Edit: Re-reading your question, it could be that your function is running before the page has finished loaded, and therefore won't execute properly; putting it in $(function) would indeed fix that!

编辑:重新阅读您的问题,可能是您的功能在页面加载完成之前正在运行,因此无法正常执行;把它放在 $(function) 中确实可以解决这个问题!

回答by cwharris

The following is a jQuery function call:

下面是一个 jQuery 函数调用:

$(...);

Which is the "jQuery function." $is a function, and $(...)is you calling that function.

这就是“jQuery 函数”。$是一个函数,$(...)你是否在调用那个函数。

The first parameter you've supplied is the following:

您提供的第一个参数如下:

function() {}

The parameter is a function that you specified, and the $function will call the supplied method when the DOM finishes loading.

参数是您指定的$函数,该函数将在 DOM 完成加载时调用提供的方法。

回答by Chris Hasiński

It's just shorthand for $(document).ready(), as in: $(document).ready(function() { YOUR_CODE_HERE });. Sometimes you have to use it because your function is running before the DOM finishes loading.

它只是 的简写$(document).ready(),如:$(document).ready(function() { YOUR_CODE_HERE });。有时您必须使用它,因为您的函数在 DOM 完成加载之前正在运行。

Everything is explained here: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

一切都在这里解释:http: //docs.jquery.com/Tutorials: Introducing_$(document).ready()

回答by Terry

I think you may be confusing Javascript with jQuery methods. Vanilla or plain Javascript is something like:

我认为您可能将 Javascript 与 jQuery 方法混淆了。Vanilla 或普通 Javascript 类似于:

function example() {
}

A function of that nature can be called at any time, anywhere.

可以随时随地调用这种性质的函数。

jQuery (a library built on Javascript) has built in functions that generally required the DOM to be fully rendered before being called. The syntax for when this is completed is:

jQuery(一个基于 Javascript 的库)内置了通常要求 DOM 在被调用之前完全呈现的函数。完成时的语法是:

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

So a jQuery function, which is prefixed with the $or the word jQuerygenerally is called from within that method.

因此,通常从该方法中调用以$或 字为前缀的 jQuery 函数jQuery

$(document).ready(function() {        
    // Assign all list items on the page to be the  color red.  
    //      This does not work until AFTER the entire DOM is "ready", hence the $(document).ready()
    $('li').css('color', 'red');   
});

The pseudo-code for that block is:

该块的伪代码是:

When the document object model $(document)is ready .ready(), call the following function function() { }. In that function, check for all <li>'s on the page $('li')and using the jQuery method .CSS() to set the CSS property "color" to the value "red" .css('color', 'red');

当文档对象模型$(document)准备好后.ready(),调用以下函数function() { }。在该函数中,检查<li>页面上的所有's$('li')并使用 jQuery 方法 .CSS() 将 CSS 属性“color”设置为值“red”.css('color', 'red');

回答by imm

This is a shortcut for $(document).ready(), which is executed when the browser has finished loading the page (meaning here, "when the DOM is available"). See http://www.learningjquery.com/2006/09/introducing-document-ready. If you are trying to call example()before the browser has finished loading the page, it may not work.

这是 的快捷方式$(document).ready(),在浏览器完成页面加载时执行(这里的意思是“当 DOM 可用时”)。请参阅http://www.learningjquery.com/2006/09/introducing-document-ready。如果您example()在浏览器完成页面加载之前尝试调用,它可能不起作用。