jQuery document.ready vs自调用匿名函数 [英] jQuery document.ready vs self calling anonymous function

查看:81
本文介绍了jQuery document.ready vs自调用匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两者之间有什么区别。

What is the difference between these two.


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

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

这两个函数是否同时被调用?
我知道,当浏览器呈现整个HTML页面时会触发document.ready,但第二个函数(自调用匿名函数)会怎样。它是否等待浏览器完成渲染页面,或者只要遇到它就会被调用?

Are these both functions called at the same time? I know, document.ready will be triggered when the entire HTML page is rendered by the browser but what about 2nd function (self calling anonymous function). Does it wait for browser to complete rendering the page or it is called whenever it is encountered?

推荐答案


  • $(document).ready(function(){...}); 或简短 $ (function(){...});

    DOM准备就绪时调用此函数这意味着,您可以开始查询元素。 .ready()将在不同的浏览器上使用不同的方式来确保DOM真的准备就绪。

    This Function is called when the DOM is ready which means, you can start to query elements for instance. .ready() will use different ways on different browsers to make sure that the DOM really IS ready.

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

    这不过是一个函数当浏览器解释您的 ecma- / javascript 时,会尽快调用。因此,您不太可能在此处成功处理 DOM元素

    That is nothing else than a function that invokes itself as soon as possible when the browser is interpreting your ecma-/javascript. Therefor, its very unlikely that you can successfully act on DOM elements here.

    这篇关于jQuery document.ready vs自调用匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆