[英]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.