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);
   }
};

相关文章:

  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2023-03-20
  • 2021-11-20
  • 2021-08-25
猜你喜欢
  • 2022-02-09
  • 2021-06-14
  • 2022-12-23
  • 2021-11-10
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案