freeCodeCamp/guide/chinese/javascript/onload-event/index.md

31 lines
951 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Onload Event
localeTitle: Onload事件
---
## Onload事件
`onload`事件用于在加载页面后立即执行JavaScript函数。
### 例:
```javascript
<body onload="myFunction()">
<script>
function myFunction() {
alert('Page finished loading');
}
</script>
```
在上面的示例中,只要加载了网页,就会调用`myFunction`函数,向用户显示`Page finished loading`警报。
`onload`事件最常用于`<body>`元素中以执行脚本。如果它附加到`<body>` 则一旦网页完全加载了所有内容图像脚本文件CSS文件等脚本就会运行。
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload)
#### 其他资源
[jQuery .on事件处理程序附件](https://api.jquery.com/on/) [Stack Overflowwindow.onload与document.onload](https://stackoverflow.com/questions/588040/window-onload-vs-document-onload)