- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
window.onload
从我的阅读中听起来它可以与 document.onload
松散地互换,但我的经验表明这是不正确的。我继承了一个 JS 脚本,但我不确定如何更正它。我希望 JS 在加载 DOM 后执行,而不是在加载所有资源后执行。我该怎么做?
目前我有:
window.onload = initDropMenu;
我试过:
document.onload = initDropMenu;
这只会导致菜单无法加载。我也试过从 JS 中完全删除该行,并让 DOM 通过以下方式执行它:
<body onload="initDropMenu()">
这也导致没有菜单,并且控制台中没有错误。我的 JS 知识有限,我在这里缺少什么?
最佳答案
在窗口加载事件:
The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
[source: developer.mozilla.org]
<script>
window.onload = function(){ init(); };
</script>
在 HTML 元素上加载事件:
The
load
event is fired when a resource and its dependent resources have finished loading.
[source: developer.mozilla.org]
<!-- When the image is loaded completely -->
<img onload="image_loaded()" src="w3javascript.gif">
<!-- When the frame is loaded completely (including all resources) -->
<iframe onload="frame_loaded()" src="about.html">
<!-- When body loaded completely (including all resources, images and iframes) -->
<body onload="init()">
许多论坛甚至本站的某些答案可能会误导您,但是 load
body 元素上的事件不仅等同于 load
窗口上的事件,它是完全相同的事件。以下引用说明了这一点。
For historical reasons, some attributes/properties on the
<body>
and<frameset>
elements actually set event handlers on their parent Window object. (The HTML specification names these: onblur, onerror, onfocus, onload, onscroll.)
[source: developer.mozilla.org]
DOMContentLoaded 事件:
开发者应该使用的是DOMContentLoaded
文档上的事件。它会在 html 加载并完全解析后触发。
document.addEventListener("DOMContentLoaded", function(event) {
alert("Document is ready");
});
The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event load should be used only to detect a fully-loaded page. It is an incredibly popular mistake to use load where DOMContentLoaded would be much more appropriate, so be cautious.
[source: developer.mozilla.org]
也许这是关于该主题的唯一具有适当引用的答案
关于javascript - JS window.onload 用法与文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092746/
所以我想隐藏一个div,所以我有我的代码 function hide(){hide div code} document.onload = hide(); 不起作用。 window.onload =
我有一个外部 js 文件,它有一个函数,该函数旨在在文档正文加载后运行。这是我到目前为止所拥有的,但它并没有像它应该的那样工作。请一些帮助。 外部JS文件 window.document.body.a
我觉得这很有趣。考虑以下代码, window.onload 和 body onload=""调用相同的函数。但是,结果不同。在我看来,window.onload 的集合有问题。这是代码:
我多次在链接上使用 Shadowbox,我知道 href 指示 Shadowbox 像这样在框中加载什么页面: 但是,我现在需要在用户进入页面时立即显示一个阴影框。不是onclick,而是onloa
我正在制作一个页眉和页脚不变的网站。单击站点时,主 div 会更改。这是代码: Item 1 ... $("#item").click(function(){$("#main").load("i
我在网页中有 2 个函数 (js) 用于将图像附加到 Canvas 元素: function attachImage(tile, x, y) { base_image = new
首先向大家问好。我是 jquery 的新手,并且在学习曲线上。 我的脚本是这样的: $(function () { $('#content').find('#nav a').on('click'
我今天遇到了一个奇怪的错误。我有一个简单的用户脚本,它添加了一个 window.onload事件到所有网站。但是其中一个网站有一个 定义。发生的事情是 window.onload像往常一样工作但是安装
今天我遇到了一个奇怪的问题: window.onload 在引用名为 onload 的函数时似乎不会触发。 我以前从未注意到这一点: 测试 1: window.onload=onload; funct
假设你想用 JS 把一个元素变成蓝色,像这样: Boy, I sure do wish I was blue 上面的行不起作用,因为 this 以该上下文中的 window 对象为目标。您可以使用 I
我的问题与此处提出的问题类似,但又略有不同,window.onload vs 那道题是用 window.onload 和内联 js 的对比。 我的问题是下面的区别。假设正文标签的 ID 为“body
window.onload事件和body标签的onload事件到底有什么区别?我什么时候使用哪个以及如何正确使用? 最佳答案 window.onload = myOnloadFunc和 是使用同一事件
哪个得到更广泛的支持:window.onload 或 document.onload? 最佳答案 他们什么时候开火? window.onload 默认情况下,它会在整个页面加载时触发,包括其内容(图像
之间有什么区别: window.onload = someFunction; window.onload = someFunction(); 括号在最后。它们有什么不同吗? 我们一般使用第一个!如果我
在 this question 的答案中,我们读到 function f() {}在本地定义名称,而 [var] f = function() {}全局定义它。这对我来说很有意义,但是这两个声明之间存
当我将索引值传递给 onLoad() 时,因此当它开始在后台运行时,它将使用索引值来索引数组。问题是,从表面上看,当 onLoad() 开始运行时,它引用的索引值始终是最后一个索引。 我尝试将每个请求
假设我有这个页面: Hello, world! test.html 包含以下内容: huge.png由于某种原因是100mb。 我无法控制 test.html,但我必须在我的页面中包含 ifram
我正在做以下工作。如果启用了 js,它基本上会通过 ?answer=1。在我添加 onload 参数之前它一直有效(因为我希望这在没有用户触发的情况下发生)。但是,添加 onload 似乎会停止(否则
我可以同时使用body onload和window.onload吗?我已经使用此代码尝试过 window.onload = alertSec; 但是没有成功。我只是需要有人向我确认一下
我可以同时使用body onload和window.onload吗?我已经使用此代码尝试过 window.onload = alertSec; 但是没有成功。我只是需要有人向我确认一下
我是一名优秀的程序员,十分优秀!