onload
当页面载入完毕后执行Javascript代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>操作对象被加载</title>
<script>
window.οnlοad=function(){
document.getElementById("s").style.color="red";
}
</script>
</head>
<body>
<span id="s">123</span>
</body>
</html>
DOMCOntentLoaded事件
指文档加载完成触发的事件,即dom加载完成,不用考虑其他资源,例如图片
例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOMCOntentLoaded事件</title>
</head>
<body>
<script>
function say(){
alert("文档结构加载完毕");
}
//添加事件监听
window.addEventListener("DOMCOntentLoaded",say);
</script>
</body>
</html>