attachEvent()   和   detachEvent() 这个两个函数接收相同参数 事件名称和函数
先说一下区别:
1:IE不支持捕获 支持冒泡
2:IE添加事件不能屏蔽重复的函数
3:IE中的this指向的是window不是DOM
4:IE无法接受event对象 但是使用了attachEvent()可以 有区别

问题一:覆盖问题  可以解决但是顺序不同于W3C 倒过来输出
window.attachEvent('onload',function(){
alert('lee');
});
window.attachEvent('onload',function(){
alert('222lee');
});

问题二:相同函数屏蔽问题  未解决
window.attachEvent('onload',init);
window.attachEvent('onload',init);
function init(){
alert('lee');
};

问题三:是否可以传递this
window.attachEvent('onload',function(){
var box = document.getElementById('box');
box.attachEvent('onclick',function(){
alert(this === window);            //不能传递this
});
});

问题四:添加额外的函数会不会覆盖   解决了
 

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------

IE事件切换器

window.attachEvent('onload',function(){
var box = document.getElementById('box');
box.attachEvent('onclick',toBlue);
});
function toRed(){
var that = window.event.srcElement;      //IE的event属性
    that.className = 'red';
    that.detachEvent('onclick',toRed);
    that.attachEvent('onclick',toBlue);
}

function toBlue(){
var that = window.event.srcElement;      //IE的event属性
    that.className = 'blue';                         //IE无法使用this传递 要用that保存然后传递
    that.detachEvent('onclick',toBlue);
    that.attachEvent('onclick',toRed);
}

 

posted on 2018-01-25 21:17  YKing_匆  阅读(138)  评论(0)    收藏  举报