$(window).load(function(){})和`$(function(){})之间有什么区别? [英] What's the difference between `$(window).load(function(){})` and `$(function(){})`

查看:129
本文介绍了$(window).load(function(){})和`$(function(){})之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用 $(window).load(function(){}); ,直到有人说我们可以使用 $(function(){}); ,它们将执行相同的操作。

但现在我有更多的经验,我注意到它们不一样。我注意到第一件事在第二段代码之后踢了一下。

我只想知道有什么区别?

I was using $(window).load(function(){}); for my projects until somewhere I saw that somebody said we could just use $(function(){}); and they would perform identically.
But now that I have more experience I have noticed that they are not identical. I noticed that the first piece kicks in a little bit after the second piece of code.
I just want to know what's the difference?

推荐答案

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

将等待文档加载(DOM树已加载),直到整个窗口加载。例如,它不会等待图像,css或javascript被完全加载。一旦DOM加载了所有的HTML组件和事件处理程序,就可以处理文档,然后$(document).ready()将完成

will wait till the document is loaded(DOM tree is loaded) and not till the entire window is loaded. for example It will not wait for the images,css or javascript to be fully loaded . Once the DOM is loaded with all the HTML components and event handlers the document is ready to be processed and then the $(document).ready() will complete

$(window).load(function(){});

这等待整个窗口加载。当整个页面被加载时,只有$(window).load()完成。因此,显然$(document).ready(function(){})在$(window).load()之前完成,因为填充组件(如图像,css)需要更多的时间才加载DOM树。

This waits for the entire window to be loaded. When the entire page is loaded then only the $(window).load() is completed. Hence obviously $(document).ready(function(){}) finishes before $(window).load() because populating the components(like images,css) takes more time then just loading the DOM tree.

所以 $(function(){}); 不能用来代替 $(window) .load(function(){});

这篇关于$(window).load(function(){})和`$(function(){})之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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