window.onload vs< body onload ="" /> [英] window.onload vs <body onload=""/>

查看:136
本文介绍了window.onload vs< body onload ="" />的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

window.onload 事件和 onload 事件之间的区别是什么 body 标签?我何时使用哪个以及如何正确使用?

What exactly is the difference between the window.onload event and the onload event of the body tag? when do I use which and how should it be done correctly?

推荐答案

window.onload = myOnloadFunc < body onload =myOnloadFunc();> 是使用同一事件的不同方式。使用 window.onload 虽然不那么突兀 - 它会将你的JavaScript从HTML中删除。

window.onload = myOnloadFunc and <body onload="myOnloadFunc();"> are different ways of using the same event. Using window.onload is less obtrusive though - it takes your JavaScript out of the HTML.

所有常见的JavaScript库,Prototype,ExtJS,Dojo,JQuery,YUI等为文档加载时发生的事件提供了很好的包装。您可以侦听窗口onLoad事件,并对此作出反应,但是在下载所有资源之前不会触发onLoad,因此在获取最后一个巨大的图像之前不会执行事件处理程序。在某些情况下,这正是您想要的,在其他情况下,您可能会发现在DOM准备就绪时进行监听更合适 - 此事件类似于onLoad,但无需等待图像等即可下载。

All of the common JavaScript libraries, Prototype, ExtJS, Dojo, JQuery, YUI, etc. provide nice wrappers around events that occur as the document is loaded. You can listen for the window onLoad event, and react to that, but onLoad is not fired until all resources have been downloaded, so your event handler won't be executed until that last huge image has been fetched. In some cases that's exactly what you want, in others you might find that listening for when the DOM is ready is more appropriate - this event is similar to onLoad but fires without waiting for images, etc. to download.

这篇关于window.onload vs&lt; body onload =&quot;&quot; /&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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