简体   繁体   English

$(document).ready(function()和$(function())有什么区别?

[英]What is the difference between $(document).ready(function() and $(function() ?

So I know what this does: 所以我知道这是做什么的:

$(document).ready(function(){
    // Your code here...
});

Now I have seen people doing this lately: 现在我看到人们这么做了:

<script type="text/javascript">     
$(function(){
    // Your code here...
});
</script>

Are these two ways of doing the same thing? 这两种方式做同样的事情吗?

I see an anonymous function being declared inside a jquery selector here, but never actually being invoked, yet by the way the page runs it seems that this may just run on pageload. 我在这里看到一个匿名函数在jquery选择器中声明,但实际上从未被调用过,但是通过页面运行的方式,似乎这可能只是在pageload上运行。

yes, they're doing the same thing. 是的,他们正在做同样的事情。 the $() function wraps $(document).ready() when the parameter to the call is a single function object. 当调用的参数是单个函数对象时, $()函数包装$(document).ready()

(Edited to reflect a question in comment) (编辑反映评论中的问题)

Yes, they are doing exactly the same thing. 是的,他们做的完全一样。

$(function(){
    // Your code here...
});

is a shortcut for 是一个快捷方式

$(document).ready(function(){
    // Your code here...
});

暂无
暂无

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

相关问题 $(document).bind(&#39;ready&#39;,function)和$(document).ready(function(){})之间有什么区别 - What are the difference between $(document).bind('ready', function) and $(document).ready(function() {}) $(callback) 和 $(document).ready(function) 的区别? - Difference between $(callback) and $(document).ready(function)? 是否在document.ready中定义函数之间的区别 - Difference between defining a function within document.ready or not 使用自调用功能和准备文档之间的区别 - The difference between using a self invoking function and document ready jQuery-$(document).ready(和$(document).on(&#39;ready page:load&#39; - jquery - what is the difference between $(document).ready( and $(document).on('ready page:load' $(document).ready(initialize)和$(document).on('ready',initialize)之间有什么区别 - what is the difference between $(document).ready(initialize) and $(document).on('ready',initialize) $(window).load和$(document).ready之间有什么区别? - What is the difference between $(window).load and $(document).ready? ondomready和jquery的ready函数有什么区别? - What is the difference between ondomready and jquery's ready function? document.ready()内部函数和函数中的区别 <script></script> 标签 - difference between document.ready() inner function and a function in <script></script> tag 将JavaScript调用放入$(document).ready(function(){和将它们放入HTML之间有区别吗? - Is there a difference between placing JavaScript calls in $(document).ready(function(){ and placing them in HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM