请高手讲讲this,以及time。

window.onload=function(){

var aA=document.getElementsByTagName("a");

for(var i=0;i<aA.length;i++){

      aA[i].onmouseover=function(){

   clearInterval(this.time);

       var This=this;

This.time=setInterval(function(){

 This.style.width=This.offsetWidth+8+"px";

 if(This.offsetWidth>=120){

   clearInterval(This.time);

}

 },30)

}

aA[i].onmouseout=function(){

   clearInterval(this.time);

       var This=this;

This.time=setInterval(function(){

 This.style.width=This.offsetWidth-8+"px";

 if(This.offsetWidth<=100){

   This.style.width="100px";

   clearInterval(This.time);

}

     },30)

}

}

}

这段代码中,this.time是什么意思?  还有为啥要定义This?



中南大学icelee
浏览 1783回答 3
3回答

zhangcl

This就是你当前的页面,应该是window,time是setInteval()这个函数的返回值,是一个数字,你可以在百度上查到的,表示这个函数用window的time变量来接收。同理可以停止setInteval()的操作,方法我忘记了 ,你可以查一下,好像是clearInteval(),然后将time=null就行了;

PHPBird

参见我曾经写的一篇文章http://www.imooc.com/article/1758

李晓健

var This=this;  是为了保存当前鼠标所在的标签, This.time=setInterval(function()  是给当前标签添加一个和time属性,用来保存应用到这个标签上的定时器。 定时器里面的 clearInterval(This.time);  是取消这个标签上的定时器。这里用This而不用 this是因为,这里的this已经不是鼠标所在有标签了,而This在上面已经定义成是当前标签。
打开App,查看更多内容
随时随地看视频慕课网APP