我做了一个简单的JS练习,里面添加了一个定时器,但是定时器没有起作用,求大神解答啊!!
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
ul{width:800px;height:30px; background:#ccc; margin:0 auto}
li{ list-style:none;}
a{float:left; text-decoration:none;color:#fff; font-family:"微软雅黑";line-height:30px;padding:0 10px}
div{width:100px;height:100px;float:left;}
#list{ width:800px;margin:0 auto}
#div1{ background:red}
#div2{ background:yellow}
#div3{ background:blue}
</style>
<script>
window.onload=function()
{
var oUl=document.getElementsByTagName('ul')[0]
var aLi=oUl.getElementsByTagName('li')
var oDiv=document.getElementById('list')
var aDiv=oDiv.getElementsByTagName('div')
var timer=null
for(var i=0;i<aLi.length;i++)
{
aDiv[i].style.display='none'
aLi[i].index=i
aLi[i].onmouseover=function()
{
if(timer)
{
clearTimeout(timer)
timer=null
}
aDiv[this.index].style.display='block'
}
aLi[i].onmouseout=function()
{
clearTimeout(timer)
timer=setTimeout(function()
{
aDiv[this.index].style.display='none'
timer=null
},1000)
}
}
}
</script>
</head>
<body>
<ul>
<li><a href="">首页</a></li>
<li><a href="">次页</a></li>
<li><a href="">尾页</a></li>
</ul>
<div id="list">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
</body>
</html> 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
ul{width:800px;height:30px; background:#ccc; margin:0 auto}
li{ list-style:none;}
a{float:left; text-decoration:none;color:#fff; font-family:"微软雅黑";line-height:30px;padding:0 10px}
div{width:100px;height:100px;float:left;}
#list{ width:800px;margin:0 auto}
#div1{ background:red}
#div2{ background:yellow}
#div3{ background:blue}
</style>
<script>
window.onload=function()
{
var oUl=document.getElementsByTagName('ul')[0]
var aLi=oUl.getElementsByTagName('li')
var oDiv=document.getElementById('list')
var aDiv=oDiv.getElementsByTagName('div')
var timer=null
for(var i=0;i<aLi.length;i++)
{
aDiv[i].style.display='none'
aLi[i].index=i
aLi[i].onmouseover=function()
{
if(timer)
{
clearTimeout(timer)
timer=null
}
aDiv[this.index].style.display='block'
}
aLi[i].onmouseout=function()
{
clearTimeout(timer)
timer=setTimeout(function()
{
aDiv[this.index].style.display='none'
timer=null
},1000)
}
}
}
</script>
</head>
<body>
<ul>
<li><a href="">首页</a></li>
<li><a href="">次页</a></li>
<li><a href="">尾页</a></li>
</ul>
<div id="list">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
</body>
</html> 展开
1个回答
展开全部
浏览器调试下,显示44行Cannot read property 'style' of undefined,其中的this指向的是window对象.
修改为:
aLi[i].onmouseout=function()
{
clearTimeout(timer)
var _this=this; //更改的地方
timer=setTimeout(function()
{
aDiv[_this.index].style.display='none';//更改的地方
timer=null
},1000)
}
修改为:
aLi[i].onmouseout=function()
{
clearTimeout(timer)
var _this=this; //更改的地方
timer=setTimeout(function()
{
aDiv[_this.index].style.display='none';//更改的地方
timer=null
},1000)
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询