這段代碼里this到底指什么,為什么把this換成ali[i]就不行了
<!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 type="text/css">
* { margin: 0; padding: 0; font-size: 14px; }
a { color: #333; text-decoration: none }
ul{ list-style: none; }
.nav {height: 30px; border-bottom: 5px solid #F60; margin-left:50px; width:600px;}
.nav li { float: left; position:relative; height:30px; width:120px }
.nav li a { display: block; height: 30px; text-align: center; line-height: 30px; width:120px; background: #efefef; margin-left: 1px; }
.subNav{ position:absolute; top:30px; left:0; width:120px; height:0; overflow:hidden}
.subNav li a{ background:#ddd }
.subNav li a:hover{ background:#efefef}
</style>
<script>
window.onload=function(){
? ? var aLi=document.getElementsByTagName('li');
for(var i=0; i<aLi.length; i++){
? aLi[i].onmouseover=function(){
? ? ? ? ? ? var oSubNav=this.getElementsByTagName('ul')[0];
? ? ? ? ? ? if(oSubNav){
? ? ? ? ? ? var This=oSubNav;
? ? ? ? ? ? clearInterval(This.time);
? ? ? ? ? ? This.time=setInterval(function(){
? ? ? ? ? ? ? ? ? ? This.style.height=This.offsetHeight+16+"px";
? ? ? ? ? ? ? ? ? ? if(This.offsetHeight>=120)
? ? ? ? ? ? ? ? ? ? clearInterval(This.time);
? ? ? ? ? ? ? ? },30)
? ? ? ? ? ? ?}
? ? ? ? ? }
? ? ? ? //鼠標(biāo)離開菜單,二級菜單動畫收縮起來。
aLi[i].onmouseout=function(){
? ? ? ? ? ? var oSubNav=this.getElementsByTagName('ul')[0];
? ? ? ? ? ? if(oSubNav){
? ? ? ? ? ? var This=oSubNav;
? ? ? ? ? ? clearInterval(This.time);
? ? ? ? ? ? This.time=setInterval(function(){
? ? ? ? ? ? ? ? ? ? This.style.height=This.offsetHeight-16+"px";
? ? ? ? ? ? ? ? ? ? if(This.offsetHeight<=0)
? ? ? ? ? ? ? ? ? ? clearInterval(This.time);
? ? ? ? ? ? ? ? },30)
? ? ? ? ? ? ?}
? ? ? ? ? }
}
}
</script>
</head>
<body>
<ul class="nav">
? ? <li><a href="#">一級菜單</a>
? ? <ul class="subNav">
? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? </ul>
? ? </li>
? ? <li><a href="#">一級菜單</a>
? ? <ul class="subNav">
? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? ? ? <li><a href="#">二級菜單</a></li>
? ? ? ? </ul>
? ? </li>
? ? <li><a href="#">一級菜單</a></li>
? ? <li><a href="#">一級菜單</a></li>
? ? <li><a href="#">一級菜單</a></li>
</ul>
</body>
</html>
2016-12-16
問題一你可以用console.log(this)打印出來的是<li>,this在aLi[i]這個鼠標(biāo)經(jīng)過的函數(shù)里,this就代表aLi[i]這個數(shù)組。問題二,不能用aLi[i]代替,循環(huán)的速度會太快
2016-10-25
代表當(dāng)前的a標(biāo)簽??!大兄弟