diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/globaleventhandlers/onload | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/globaleventhandlers/onload')
-rw-r--r-- | files/zh-cn/web/api/globaleventhandlers/onload/index.html | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/globaleventhandlers/onload/index.html b/files/zh-cn/web/api/globaleventhandlers/onload/index.html new file mode 100644 index 0000000000..a9a56d9c6b --- /dev/null +++ b/files/zh-cn/web/api/globaleventhandlers/onload/index.html @@ -0,0 +1,79 @@ +--- +title: GlobalEventHandlers.onload +slug: Web/API/GlobalEventHandlers/onload +tags: + - DOMContentLoaded + - ES6 + - GlobalEventHandlers.onload + - load + - onload +translation_of: Web/API/GlobalEventHandlers/onload +--- +<p>{{ ApiRef() }}</p> + +<p>{{domxref("GlobalEventHandlers")}} mixin 的 <code>onload</code> 属性是一个事件处理程序用于处理{{domxref("Window")}}, {{domxref("XMLHttpRequest")}}, {{htmlelement("img")}} 等元素的加载事件,当资源已加载时被触发。</p> + +<h3 id="Syntax" name="Syntax">语法</h3> + +<pre class="eval">window.onload = <em>funcRef</em>; +</pre> + +<ul> + <li> 当 window <code style="font-style: normal;">load</code> 事件触发时,<span style="font-family: 'Courier New','Andale Mono',monospace;">funcRef</span> 方法会被调用。</li> +</ul> + +<h3 id="Value">Value</h3> + +<p>funcRef 是窗口加载事件触发时调用的处理函数。</p> + +<h3 id="Example" name="Example">示例</h3> + +<pre class="brush: js">window.onload = function() { + init(); + doSomethingElse(); +}; +</pre> + +<pre><code><!doctype html> +<html> + <head> + <title>onload test</title> + // ES5 + <script> + function load() { + console.log("load event detected!"); + } + window.onload = load; + </script> + // ES2015(aka: ES6) + <script> + const load = () => { + console.log("load event detected!"); + } + window.onload = load; + </script> + </head> + <body> + <p>The load event fires when the document has finished loading!</p> + </body> +</html></code></pre> + +<h3 id="Notes" name="Notes">注意</h3> + +<p>在文档装载完成后会触发 <code>load</code> 事件。此时,在文档中的所有对象都在DOM中,所有图片,脚本,链接以及子框都完成了装载。 </p> + +<p>同时也会有 <a href="/en/Gecko-Specific_DOM_Events">Gecko-指定 DOM事件</a>,如 <code>DOMContentLoaded</code> 和 <code>DOMFrameContentLoaded</code> (它们可以使用 {{ domxref("EventTarget.addEventListener()") }} 来处理 ) , 这些事件在页面DOM构建起来后就会触发,而不会等到其他的资源都装载完成。 </p> + +<h3 id="Specification" name="Specification">规范</h3> + +<p>这个事件处理程序在 <a class="external" href="http://www.whatwg.org/html/#handler-window-onload" title="http://www.whatwg.org/html/#handler-window-onload">HTML</a> 中指定。</p> + +<h3 id="参见">参见</h3> + +<ul> + <li><code><a href="https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded">监听事件:简单的DOM事件</a>中的 DOMContentLoaded</code> 事件 </li> + <li><code>DOMContentLoaded</code> event in <a href="https://developer.mozilla.org/en-US/docs/Listening_to_events_in_Firefox_extensions#Simple_DOM_events">Listening to events: Simple DOM events</a></li> + <li>IIEF <a href="https://en.wikipedia.org/wiki/Immediately-invoked_function_expression" rel="nofollow noreferrer">Immediately-invoked function expression</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded">DOMContentLoaded</a></li> + <li> </li> +</ul> |