1-1 onload是页面加载完毕之后触发
window.onload = function(){
var p =document.getElementById("p");
console.log(p)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/index.js"></script>
</head>
<body>
<p id="p">hello world</p>
</body>
</html>
1-2 onresize 窗口大小改变时触发
<script >
window.onresize = function(){
console.log("窗口改变了")
}
</script>
1-3 onscroll 窗口滚动时触发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
height: 2500px;
background-color: aliceblue;
}
</style>
</head>
<body>
<div>
</div>
<script>
window.onscroll = function(){
console.log(1)
}
</script>
</body>
</html>
1-4定时器
<body>
<script>
setTimeout(function(){
console.log("hello")
},1000)
setInterval(function(){
console.log("world")
},2000)
</script>
</body>