IE8中使用attachEvent绑定事件处理函数时,不能直接向event 对象添加数据属性。可以用属性复制的方法,包装新的event对象。
1. 属性复制
var ObjectExtend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
2. 绑定事件
var attachEvent = function (obj,ev,func,data){
if(window.attachEvent){
obj.attachEvent('on'+ev,function(event){
var _event = ObjectExtend({},window.event || event);
if(data && _event)_event['data'] = data;
func(_event);
});
}else if(window.addEventListener){
obj.addEventListener(ev,function(event){
if(data)event['data'] = data;
func(event);
},true);
}
};