为了账号安全,请及时绑定邮箱和手机立即绑定

关于this问题

关于this问题

window.onload=function(){ var aLi =document.getElementById("li1"); aLi.onmousemove=function(){   //  必须是aLi,不能是this???? startMove(aLi,'width',200,function(){ startMove(aLi,'height',200,function(){ startMove(aLi,'opacity',100); }); }); } }在startMove()中为什么aLi都写成this不行呢,控制台显示Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.191move.js:9 Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.这错误。全部改成aLi却正常能够运行。
查看完整描述

5 回答

?
Perona

TA贡献355条经验 获得超403个赞

要学会调试,你控制台输出一下this就知道这里的this是不是指代变量aLi

查看完整回答
2 反对 回复 2016-01-26
?
小红红乖乖

TA贡献5条经验 获得超1个赞

如果你在问题那个地方用了this,这个this是在window.onload=function(){}里面的,它代表的就是window.onload这个东东;再如在aLi.onmousemove=function(){ }里面用this,就代表了aLi.onmousemove。看用它的时候包括它的是什么,这只适用于这个例子啊。好像其它地方的又有些不同,百度上还有很多情况,我感觉我这个就是一个小范围的取巧啊

查看完整回答
反对 回复 2016-01-27
?
js_saber

TA贡献1条经验 获得超0个赞

因为startmove形式了闭包,作用域跳出到Window下,因此这里使用this代表Window而不是ali

查看完整回答
反对 回复 2016-01-27
  • 5 回答
  • 0 关注
  • 4551 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信