From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/globaleventhandlers/onload/index.html | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 files/zh-cn/web/api/globaleventhandlers/onload/index.html (limited to 'files/zh-cn/web/api/globaleventhandlers/onload') 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 +--- +

{{ ApiRef() }}

+ +

{{domxref("GlobalEventHandlers")}} mixin 的 onload 属性是一个事件处理程序用于处理{{domxref("Window")}}, {{domxref("XMLHttpRequest")}}, {{htmlelement("img")}} 等元素的加载事件,当资源已加载时被触发。

+ +

语法

+ +
window.onload = funcRef;
+
+ + + +

Value

+ +

funcRef 是窗口加载事件触发时调用的处理函数。

+ +

示例

+ +
window.onload = function() {
+  init();
+  doSomethingElse();
+};
+
+ +
<!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>
+ +

注意

+ +

在文档装载完成后会触发  load 事件。此时,在文档中的所有对象都在DOM中,所有图片,脚本,链接以及子框都完成了装载。 

+ +

同时也会有 Gecko-指定 DOM事件,如 DOMContentLoaded 和 DOMFrameContentLoaded (它们可以使用 {{ domxref("EventTarget.addEventListener()") }} 来处理 ) , 这些事件在页面DOM构建起来后就会触发,而不会等到其他的资源都装载完成。 

+ +

规范

+ +

这个事件处理程序在  HTML 中指定。

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf