关于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;
        }
        
阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题