什么$(function(){});做? [英] What does $(function() {} ); do?
问题描述
有时我会创建一个函数并稍后调用该函数。
Sometimes I make a function and call the function later.
示例:
function example { alert('example'); }
example(); // <-- Then call it later
不知何故,某些函数无法调用。我必须在里面调用这些函数:
Somehow, some functions cannot be called. I have to call those functions inside:
$(function() { });
$(function(){});
和(function(){});
是什么意思,它们的区别/目的是什么?
What do $(function() {});
and (function() { });
mean, and what's the difference/purpose of these?
推荐答案
$(function() { ... });
只是jQuery的简写
is just jQuery short-hand for
$(document).ready(function() { ... });
它的设计(除其他事项外)确保在所有DOM之后调用您的函数页面的元素已经可以使用了。
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.
但是,我认为这不是你遇到的问题 - 你能澄清你的意思'不知何故,有些函数是无法调用的,我必须在里面调用那些函数?
也许发布一些代码来显示哪些内容没有按预期工作?
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 ?
编辑:重新阅读你的问题,可能是你的功能在页面之前运行已完成加载,因此无法正常执行;把它放在$(函数)中确实可以解决这个问题!
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(){});做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!