根据案例来了解一下window.onload的使用
案例一
html>
<meta>
<title></title>
<style>
p{
width: 100px;
height: 100px;
background: red;
margin-left: 50px;
margin-top: 30px;
display: none;
}
</style>
<script>//
window.onload=function(){//
function btn(obj){//
var box=document.getElementById(obj);//
box.style.display="block";//
}//
}
function btn(obj){
var box=document.getElementById(obj);
box.style.display="block";
} </script>
<button>显示第一个盒子</button>
<button>显示第二个盒子</button>
<button>显示第三个盒子</button>
<button>显示第四个盒子</button>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/3522" title="绘蛙AI高清"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/178599466766244.png" alt="绘蛙AI高清" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/3522" title="绘蛙AI高清" class="overflowclass">绘蛙AI高清</a>
<p class="overflowclass">绘蛙AI高清是一款AI图片处理工具,绘蛙推出的 AI 图像高清修复工具。</p>
</div>
<a rel="nofollow" href="/ai/3522" title="绘蛙AI高清" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
这种情况下,如果把js放在window.onload里面会报错。 
解析:放在onload中的js会等html加载完再执行函数,但是在加载html的时候,里面有onclick="btn('box1')",会找不到这个函数(由于这个时候函数还没有加载)。要调用这个函数,就要让它加载完毕。
案例二
html> <meta> <title>111</title> <style> * {
margin: 0;
padding: 0;
list-style-type: none;
}
#box {
width: 360px;
padding-top: 360px;
background:url(image/01big.jpg) no-repeat center top;
margin: 100px auto;
border: 1px solid blueviolet;
overflow: hidden;
}
ul {
border-top: 1px solid blueviolet;
}
li {
float: left;
}
</style> <script> window.onload=function() {
var box =document.getElementById("box");
function fun(id,bg) {
var ID =document.getElementById(id);
ID.onmouseover=function() {
box.style.backgroundImage=bg;
}
} fun("id1","url(image/01big.jpg)");
fun("id2","url(image/02big.jpg)");
fun("id3","url(image/03big.jpg)");
fun("id4","url(image/04big.jpg)");
fun("id5","url(image/05big.jpg)");
}
<script>
window.onload=function(){
var box =document.getElementById("box"); function fun(id,bg){
var ID =document.getElementById(id);
ID.onmouseover=function(){
box.style.backgroundImage=bg;
}
}
fun("id1","url(image/01big.jpg)");
fun("id2","url(image/02big.jpg)");
fun("id3","url(image/03big.jpg)");
fun("id4","url(image/04big.jpg)");
fun("id5","url(image/05big.jpg)");
} </script>
<p>
</p>










