多个$(document).ready函数 [英] Multiple $(document).ready functions

查看:107
本文介绍了多个$(document).ready函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有多个 $(document).ready(...)函数,它们是否相互覆盖?为了辩论的缘故,假装正确的编码被扔在这扇门上。



假设我有一个 $(document).ready(function(){...}); in我的网站的脚本文件。然后我使用第三方插件,它也使用 $(document).ready(function(){...}); 。这会覆盖我已经创建的函数,或者在文档准备好后jQuery将这些函数排队到所有运行? 解决方案

否,他们不会互相推翻。



你当然可以自己检查一下: http://jsfiddle.net/6jgGt/



或者从 jQuery代码本身:

255行是ready函数,其中 jQuery.bindReady(); 被调用,除了别的以外,它还用<$ c $初始化429行的 readyList c> readyList = jQuery._Deferred();



一旦它是一个延迟对象,传入的函数就是追加的 with readyList.done(fn); ,我们可以看到在第41行的 done 方法中使用回调函数将该元素添加到数组中。推(elem); 因此每一个单独保存...


If I have multiple $(document).ready(...) functions, do they overwrite each other? For the sake of argument, pretend proper coding is thrown out the door on this one.

Say I have a $(document).ready(function() {...}); in my site's script file. Then I use a third party plugin that also uses $(document).ready(function() {...});. Will this overwrite my already created function or does jQuery "queue" these functions to all run when the document is ready?

解决方案

No, they do not override each other. Each function is executed.

You could of course check this easily yourself: http://jsfiddle.net/6jgGt/

Or understand from the jQuery code itself:

Line 255 is the ready function where the jQuery.bindReady(); is called which among other things initialises the readyList object on line 429 with readyList = jQuery._Deferred();

And once it's a deferred object the function passed in is appended with readyList.done( fn ); and we can see in the done method on line 41 that the element is added to an array with callbacks.push( elem ); so each one is saved separately...

这篇关于多个$(document).ready函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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