為什么我的代碼中的 aLi[i].timer = null; 出錯(cuò) 為什么我的代碼中的 aLi[i].timer = null; 出錯(cuò) 為什么我的代碼中的 aLi[i].timer = null; 出錯(cuò)
為什么我的代碼中的 ?aLi[i].timer = null; ?
在控制臺(tái)會(huì)出現(xiàn)錯(cuò)誤!TypeError: aLi[i] is undefined
為什么我的代碼中的 ?aLi[i].timer = null; ?
在控制臺(tái)會(huì)出現(xiàn)錯(cuò)誤!TypeError: aLi[i] is undefined
2015-07-22
舉報(bào)
2015-08-10
粗略的看了下你的定時(shí)器聲明是在局部,不是全局聲明,意思就是,你的xx[i].timer=null;是在onmouseover里聲明的,作用域問(wèn)題
2015-07-22
首先 ?alert(aLi[i]); 后是正確的,表示為object HTMLElement.
其次,我的代碼是和視頻基本一樣的
window.onload = function(){
var aLi = document.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
alert(aLi[i]);
aLi[i].onmouseover = function(){
aLi[i].timer = null;
? ?//var timer = null;
startMove(this,400);
}
aLi[i].onmouseout = function(){
startMove(this,200);
}
}
}
//var timer = null; //定義定時(shí)器,定時(shí)器是公用的
function startMove(obj,target){
clearInterval(obj.timer); //取消定時(shí)器
obj.timer = setInterval(function(){
var speed = (target - obj.offsetWidth)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth == target){
clearInterval(obj.timer);
}else{
obj.style.width = obj.offsetWidth + speed + 'px';
}
},30)
}
??
2015-07-22
首先你得看看上面你的選擇器 aLi是否正確,這里表示aLi都是undefined。alert出來(lái)看看
其次,問(wèn)問(wèn)題的時(shí)候建議把代碼段貼出來(lái),這樣人家看了代碼就能很好的找到錯(cuò)誤點(diǎn)。