window.onload 与 document.onload [英] window.onload vs document.onload

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

问题描述

哪个更受广泛支持:window.onload 还是 document.onload?

Which is more widely supported: window.onload or document.onload?

推荐答案

他们什么时候开火?

window.onload

  • 默认情况下,它会在整个页面加载时触发,包括其内容(图像、CSS、脚本等).
  • By default, it is fired when the entire page loads, including its content (images, CSS, scripts, etc.).

在某些浏览器中,它现在接管了 document.onload 的角色,并在 DOM 准备就绪时触发.

In some browsers it now takes over the role of document.onload and fires when the DOM is ready as well.

document.onload

  • 在 DOM 准备好时调用它,可以在加载图像和其他外部内容之前.

window.onload 似乎是最广泛支持的.事实上,一些最现代的浏览器在某种意义上已经将 document.onload 替换为 window.onload.

window.onload appears to be the most widely supported. In fact, some of the most modern browsers have in a sense replaced document.onload with window.onload.

浏览器支持问题很可能是许多人开始使用诸如 jQuery 之类的库来处理检查的原因准备好文档,如下所示:

Browser support issues are most likely the reason why many people are starting to use libraries such as jQuery to handle the checking for the document being ready, like so:

$(document).ready(function() { /* code here */ });
$(function() { /* code here */ });

<小时>

为了历史的目的.window.onload vs body.onload:

codingforums 上有人问过类似的问题一段时间回到关于 window.onload 超过 body.onload 的用法.这结果似乎是你应该使用 window.onload 因为它是很好地将您的结构与动作分开.

A similar question was asked on codingforums a while back regarding the usage of window.onload over body.onload. The result seemed to be that you should use window.onload because it is good to separate your structure from the action.

这篇关于window.onload 与 document.onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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