//W3C、IE事件切换器兼容函数
function addEvent(obj , type , fn){ //添加事件兼容
if(obj.addEventListener){
obj.addEventListener(type,fn);
}else if(obj.attachEvent){
obj.attachEvent('on' + type , fn);
}
}
function removeEvent(obj , type , fn){ //移除事件兼容
if(obj.removeEventListener){
obj.removeEventListener(type , fn);
}else if(obj.detachEvent){
obj.detachEvent('on' + type , fn);
}
}
function getTarget(evt){ //得到事件目标
if(evt.target){
return evt.target;
}else if(window.event.srcElement){
return window.event.srcElement;
}
}
addEvent(window , load , function(){
var box = document.getElementById('box');
addEvent(box, 'click' , toBlue);
});
function toRed(evt){
var that = getTarget(evt);
that.className = 'red';
removeEvent(that , 'click' , toRed);
addEvent(that , 'click' ,toBlue);
}
function toBlue(evt){
var that = getTarget(evt); //通过window.event.srcElement赋值给变量that
that.className = 'blue';
removeEvent(that , 'click' , toBlue);
addEvent(that , 'click' ,toRed);
}