文档事件

一 概念


- 文档事件由window调用

```html
onload:页面加载成功
onbeforeunload:页面退出或刷新警告,需要设置回调函数返回值,返回值随意

 

二 代码示范

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>文档事件</title>
<!-- 代码自身至下解析 -->
<script type="text/javascript">
var div = document.querySelector('div');
console.log(div); // null
</script>
<script type="text/javascript">
// 文档加载完毕,触发
window.onload = function () {
var div = document.querySelector('div');
console.log(div);
}
</script>
</head>
<body>
<div class="div"></div>
</body>
<script type="text/javascript">
window.onbeforeunload = function () {
return false;
}
</script>
</html>

posted @ 2018-10-18 18:40  不沉之月  阅读(93)  评论(0编辑  收藏  举报