为什么window.onload工作而document.onload不工作? [英] Why does window.onload work while document.onload doesn't?
问题描述
任何人都可以告诉我为什么以下页面在加载时不会触发警报?如果我使用 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 $ c $中的一个或另一个分配了一个函数c>或
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
):
-
window .onload === onload
-
window.onload!== document.onload
-
window!== document
window.onload === onload
window.onload !== document.onload
window !== document
这篇关于为什么window.onload工作而document.onload不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!