首页 > 关于attachEvent的小问题。

关于attachEvent的小问题。

 //一个div红蓝切换,
     window.attachEvent('onload',function(){
        var box=document.getElementById('box');
        box.attachEvent('onclick',toBlue)
    })

    function toBlue(){
        var that =window.event.srcElement;
        that.className='blue';
        that.detachEvent('onclick',toBlue);
        that.attachEvent('onclick',toRed);     
    }
    function toRed(){
        var that =window.event.srcElement;
        that.className='red';
        that.detachEvent('onclick',toRed);
        that.attachEvent('onclick',toBlue);
    }

//-------------------------------------------------- 以上可以顺利执行

 
 //不过当我这样改了之后,ie9之前版本就执行不了了。那么问题就来了,这是为什么。
    window.attachEvent('onload',function(){
            var box=document.getElementById('box');
            box.attachEvent('onclick',toBlue)
        })
        function toBlue(){
            var that =window.event.srcElement;
            that.className='blue';
            that.onclick=toRed;
        }
        function toRed(){
            var that =window.event.srcElement;
            that.className='red';    
            that.onclick=toBlue;
        }
        

https://.com/q/10...

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