关于IE下attachEvent不能绑定this的问题

由于帖子" 关于IE和Firefox下event乱谈"的回复中有好人提到IE下attachEvent不能绑定this的问题,并给出了一些解决方案,这里收集一段代码:
代码来源: http://therealcrisp.xs4all.nl/upload/addEvent.html#
js 代码
  1. /**  
  2.   * Crossbrowser event handling functions.  
  3.   *  
  4.   * A set of functions to easily attach and detach event handlers to HTML elements.  
  5.   * These functions work around the shortcomings of the traditional method 
      * ( element.onevent = function; )
     
  6.   * where only 1 handler could be attached for a certain event on the object, and
      * mimic the DOM level 2
     
  7.   * event methods addEventListener and removeEventListener for browsers that do not 
      * support these
     
  8.   * methods (e.g. Internet Explorer) without resorting to propriety methods such as
      * attachEvent and detachEvent
     
  9.   * that have a whole set of their own shortcomings.  
  10.   * Created as an entry for the 'contest' at quirksmode.org: 
      * http://www.quirksmode.org/blog/archives/2005/09/addevent_recodi.html
     
  11.   *  
  12.   * @author Tino Zijdel ( crisp@xs4all.nl )  
  13.   * @version 1.0  
  14.   * @date 2005-09-09  
  15.   */  
  16.   
  17.   
  18. /**  
  19.   * addEvent  
  20.   *  
  21.   * Generic function to attach event listeners to HTML elements.  
  22.   * This function does NOT use attachEvent but creates an own stack of function 
      * references
     
  23.   * in the DOM space of the element. This prevents closures and therefor possible 
      * memory leaks.
     
  24.   * Also because of the way the function references are stored they will get executed 
      * in the
     
  25.   * same order as they where attached - matching the behavior of addEventListener.  
  26.   *  
  27.   * @param obj The object to which the event should be attached.  
  28.   * @param evType The eventtype, eg. 'click', 'mousemove' etcetera.  
  29.   * @param fn The function to be executed when the event fires.  
  30.   * @param useCapture (optional) Whether to use event capturing, or event bubbling 
      * (default).
     
  31.   */  
  32. function  addEvent(obj, evType, fn, useCapture)   
  33. {   
  34.      //-- Default to event bubbling   
  35.      if  (!useCapture) useCapture =  false ;   
  36.   
  37.      //-- DOM level 2 method   
  38.      if  (obj.addEventListener)   
  39.     {   
  40.         obj.addEventListener(evType, fn, useCapture);   
  41.     }   
  42.      else   
  43.     {   
  44.          //-- event capturing not supported   
  45.          if  (useCapture)   
  46.         {   
  47.             alert('This browser does not support event capturing!');   
  48.         }   
  49.          else   
  50.         {   
  51.              var  evTypeRef = '__' + evType;   
  52.   
  53.              //-- create function stack in the DOM space of the element; seperate stacks for each 
      event type
      
  54.              if  (!obj[evTypeRef])   
  55.             {   
  56.                  //-- create the stack if it doesn't exist yet   
  57.                 obj[evTypeRef] = [];   
  58.   
  59.                  //-- if there is an inline event defined store it in the stack   
  60.                  var  orgEvent = obj['on'+evType];   
  61.                  if  (orgEvent) obj[evTypeRef][0] = orgEvent;   
  62.   
  63.                  //-- attach helper function using the DOM level 0 method   
  64.                 obj['on'+evType] = IEEventHandler;   
  65.             }   
  66.              else   
  67.             {   
  68.                  //-- check if handler is not already attached, don't attach the same function twice to match 
    behavior of addEventListener
      
  69.                  for  ( var  ref  in  obj[evTypeRef])   
  70.                 {   
  71.                      if  (obj[evTypeRef][ref] === fn)  return ;   
  72.                 }   
  73.             }   
  74.   
  75.              //-- add reference to the function to the stack   
  76.             obj[evTypeRef][obj[evTypeRef].length] = fn;   
  77.         }   
  78.     }   
  79. }   
  80.   
  81. /**  
  82.   * removeEvent  
  83.   *  
  84.   * Generic function to remove previously attached event listeners.  
  85.   *  
  86.   * @param obj The object to which the event listener was attached.  
  87.   * @param evType The eventtype, eg. 'click', 'mousemove' etcetera.  
  88.   * @param fn The listener function.  
  89.   * @param useCapture (optional) Whether event capturing, or event bubbling 
      * (default) was used.
     
  90.   */  
  91. function  removeEvent(obj, evType, fn, useCapture)   
  92. {   
  93.      //-- Default to event bubbling   
  94.      if  (!useCapture) useCapture =  false ;   
  95.   
  96.      //-- DOM level 2 method   
  97.      if  (obj.removeEventListener)   
  98.     {   
  99.         obj.removeEventListener(evType, fn, useCapture);   
  100.     }   
  101.      else   
  102.     {   
  103.          var  evTypeRef = '__' + evType;   
  104.   
  105.          //-- Check if there is a stack of function references for this event type on the object   
  106.          if  (obj[evTypeRef])   
  107.         {   
  108.              //-- iterate through the stack   
  109.              for  ( var  ref  in  obj[evTypeRef])   
  110.             {   
  111.                  //-- if function reference is found, remove it   
  112.                  if  (obj[evTypeRef][ref] === fn)   
  113.                 {   
  114.                      try   
  115.                     {   
  116.                          delete  obj[evTypeRef][ref];   
  117.                     }   
  118.                      catch (e)   
  119.                     {   
  120.                         obj[evTypeRef][ref] =  null ;   
  121.                     }   
  122.   
  123.                      return ;   
  124.                 }   
  125.             }   
  126.         }   
  127.     }   
  128. }   
  129.   
  130. /**  
  131.   * IEEventHandler  
  132.   *   
  133.   * IE helper function to execute the attached handlers for events.  
  134.   * Because of the way this helperfunction is attached to the object (using the DOM 
      * level 0 method)
     
  135.   * the 'this' keyword will correctely point to the element that the handler was 
      * defined on.
     
  136.   *  
  137.   * @param e (optional) Event object, defaults to window.event object when not passed
      * as argument (IE).
     
  138.   */  
  139. function  IEEventHandler(e)   
  140. {   
  141.     e = e || window.event;   
  142.      var  evTypeRef = '__' + e.type;   
  143.   
  144.      //-- check if there is a custom function stack defined for this event type on the object   
  145.      if  ( this [evTypeRef])   
  146.     {   
  147.          //-- iterate through the stack and execute each function in the scope of the object by using
     function.call
      
  148.          for  ( var  ref  in   this [evTypeRef])   
  149.         {   
  150.              if  (Function.call)   
  151.             {   
  152.                  this [evTypeRef][ref].call( this , e);   
  153.             }   
  154.              else   
  155.             {   
  156.                  //-- IE 5.0 doesn't support call or apply, so use this   
  157.                  this .__fn =  this [evTypeRef][ref];   
  158.                  this .__fn(e);   
  159.                  this .__fn =  null ;   
  160.             }   
  161.         }   
  162.     }   
  163. }  
Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值