首页 > 关于创建对象的事件触发失败?

关于创建对象的事件触发失败?

oStart.onclick=function()
{
    clearTimeout(timer);
    timer=setTimeout(function game(){
        oCont.innerHTML='<div><img src="'+ arr[Math.round((arr.length-1)*Math.random())] +'"></div>';
        var aDiv=oCont.getElementsByTagName('div')[0];
        aDiv.style.left=Math.round(800*Math.random())+'px';
        doMove(aDiv,'top',1,480,function(){
            shake(oDiv,'top',function(){
                timer=setTimeout(game,100)
            });
        });
        aDiv.onclick=function()//为何这里无法触发aDiv的点击事件?
        {
            alert(1)
        }
    },100)
}

请问大神们,为何aDiv的点击事件无法触发,很困惑,求解答,谢谢!


作用域的问题。
你的aDiv是在setTimeout里定义的,在外面aDiv是undefined,当然绑定不上事件了。

【热门文章】
【热门文章】