关于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却正常能够运行。

qq_ToutAn諾尘_0
浏览 4555回答 5
5回答

Perona

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

小红红乖乖

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

js_saber

因为startmove形式了闭包,作用域跳出到Window下,因此这里使用this代表Window而不是ali
打开App,查看更多内容
随时随地看视频慕课网APP