window.onload的位置什么讲究
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>选项卡</title>
<style type="text/css">
div{
width: 210px;
height: 210px;
background: #CCC;
}
.liang{background: yellow;}
</style>
<script type="text/javascript">
var arr=["李","明","最","帅!"];
var i=0;
var temp='';
var oBtn=document.getElementsByTagName('input');
function show(){//制作定时器执行的函数
var oDiv=document.getElementById('div1');
for (var j=0;j<oBtn.length;j++)
{
oBtn[j].className='';
};
oBtn[i].className='liang';
oDiv.innerHTML=arr[i++];
if(i==oBtn.length)
i=0;
};
window.onload=function(){ //把这句放在最前面,却会错,为什么
var oDiv=document.getElementById('div1');
for (var i=0;i<oBtn.length;i++)
{
oBtn[i].index=i;
oBtn[i].onmouseover=function (){
clearInterval(temp);
for (var j=0;j<oBtn.length;j++)
{
oBtn[j].className='';
}
this.className='liang';
oDiv.innerHTML=arr[this.index];
}
oBtn[i].onmouseout=function(){
temp=setInterval(show,1000);
}
};
clearInterval(temp);//调用之前先关掉,否则累加
temp=setInterval(show,1000);
show();
};
</script>
</head>
<body>
<input type="button" id="" value="按钮1" />
<input type="button" id="" value="按钮2" />
<input type="button" id="" value="按钮3" />
<input type="button" id="" value="按钮4" />
<div id="div1">李</div>
</body>
</html>
把window.onload放在最前面,却会错,为什么