对于this的不懂
我先是用self保存aLi[i]的this对象,self.time = setInterval()和subNav.time = setInterval()的区别?对于this不是很懂
我先是用self保存aLi[i]的this对象,self.time = setInterval()和subNav.time = setInterval()的区别?对于this不是很懂
2015-10-24
<script> window.onload=function(){ var aLi=document.getElementsByTagName('li'); for(var i=0; i<aLi.length; i++){ aLi[i].onmouseover=function(){ //鼠标经过一级菜单,二级菜单动画下拉显示出来 self = this; var subNav = self.getElementsByTagName("ul")[0]; if(subNav){ clearInterval(subNav.time); subNav.time = setInterval(function(){//原先写成self.time就有错误了 subNav.style.height = subNav.offsetHeight + 16 +"px"; if(subNav.offsetHeight >= 120){ clearInterval(subNav.time); } },30); } } //鼠标离开菜单,二级菜单动画收缩起来。 aLi[i].onmouseout=function(){ self = this; var subNav = self.getElementsByTagName("ul")[0]; if(subNav){ clearInterval(subNav.time); subNav.time = setInterval(function(){ subNav.style.height = subNav.offsetHeight - 16 +"px"; if(subNav.offsetHeight == 0){ clearInterval(subNav.time); } },30); } } } } </script>
举报