为什么window.onload工作而document.onload不工作? [英] Why does window.onload work while document.onload doesn't?

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

问题描述

任何人都可以告诉我为什么以下页面在加载时不会触发警报?如果我使用 window.onload 而不是 document.onload 它可以工作。为什么会出现这种差异?

Can anyone tell me why the following page doesn't trigger an alert when it loads? If I use window.onload instead of document.onload it works. Why is there this difference?

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

document.onload = function() {
    alert('Test');
}

</script>
</head>
<body>
</body>
</html>


推荐答案

最简单的答案是它没有设计那样。浏览器在 window.onload 的功能。 =noreferrer>文档加载过程结束。它不会尝试执行附加到 document.onload 的函数。

The simplest answer is that it just wasn't designed that way. The browser executes the function attached to window.onload at the "end of the document loading process". It does not attempt to execute a function attached to document.onload.

可以 document.onload 分配一个函数,但浏览器不会对它做任何特殊操作。

You could assign a function to document.onload but the browser will not do anything special with it.

要记住一些事情(假设你刚刚为 window.onload document.onload ):

Some things to keep in mind (assuming you've just assigned a function to one or the other of window.onload or document.onload):


  1. window .onload === onload

  2. window.onload!== document.onload

  3. window!== document

  1. window.onload === onload
  2. window.onload !== document.onload
  3. window !== document

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

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