值得推荐的事件捕获函数AddEvent()

1, 下面是JQuery之父推荐的添加移除事件方法。

Java代码 复制代码
  1. /*
  2. *obj要绑定事件的对象
  3. *type事件类型,指"click","load","submit","mouseover"等
  4. *fn函数名
  5. */
  6. functionaddEvent(obj,type,fn)
  7. {
  8. if (obj.attachEvent)
  9. {
  10. obj[ 'e' +type+fn]=fn;
  11. obj[type+fn]=function()
  12. {
  13. obj[ 'e' +type+fn](window.event);
  14. }
  15. obj.attachEvent( 'on' +type,obj[type+fn]);
  16. }
  17. else
  18. obj.addEventListener(type,fn, false );
  19. }
  20. /*
  21. *obj要删除事件的对象
  22. *type事件类型,指"click","load","submit","mouseover"等
  23. *fn函数名
  24. */
  25. functionremoveEvent(obj,type,fn)
  26. {
  27. if (obj.detachEvent)
  28. {
  29. obj.detachEvent( 'on' +type,obj[type+fn]);
  30. obj[type+fn]= null ;
  31. }
  32. else
  33. obj.removeEventListener(type,fn, false );
  34. }
  35. //addEvent(document.getElementById('foo'),'click',doSomething);

2.《Pro JavaScript Techniques》来看,John Resig大大推荐了Dean Edwards的addEvent()方法,而没推荐自己更精简的作品,他这样评价Edwards的addEvent()的:

1 ,可以在所有浏览器中工作,就算是古老得无任何支持的浏览器;
  1. 2 this 关键字可在所有的绑定函数中使用,指向的是当前元素;
  2. 3 ,中和了所有防止浏览器默认行为和阻止事件冒泡的各种浏览器特定函数;
  3. 4 ,不管浏览器类型,事件对象总是作为第一个对象传入;
  4. 5 ,唯一缺点是仅工作在冒泡阶段(因为深入使用事件绑定的传统方法)。
  5. 6 ,addEvent/removeEvent函数如此强大,绝对没有任何理由不在你的代码中使用。
1, 可以在所有浏览器中工作,就算是古老得无任何支持的浏览器;
2, this关键字可在所有的绑定函数中使用,指向的是当前元素;
3, 中和了所有防止浏览器默认行为和阻止事件冒泡的各种浏览器特定函数;
4, 不管浏览器类型,事件对象总是作为第一个对象传入;
5, 唯一缺点是仅工作在冒泡阶段(因为深入使用事件绑定的传统方法)。
6, addEvent/removeEvent函数如此强大,绝对没有任何理由不在你的代码中使用。



而Dean Edwards这样说:
My solution is very different.

* it performs no object detection
* it does not use the addeventListener/attachEvent methods
* it keeps the correct scope (the this keyword)
* it passes the event object correctly
* it is entirely cross-browser (it will probably work on IE4 and NS4)
* and from what I can tell it does not leak memory

下面是Dean Edwards的addEvent/removeEvent函数:

Java代码
  1. //http://dean.edwards.name/weblog/2005/10/add-event/
  2. functionaddEvent(element,type,handler){
  3. if (element.addEventListener){
  4. element.addEventListener(type,handler, false );
  5. } else {
  6. //为每一个事件处理函数赋予一个独立的ID
  7. if (!handler.$$guid)handler.$$guid=addEvent.guid++;
  8. //为元素建立一个事件类型的Hash表
  9. if (!element.events)element.events={};
  10. //为每对元素/事件建立一个事件处理函数的Hash表
  11. varhandlers=element.events[type];
  12. if (!handlers){
  13. handlers=element.events[type]={};
  14. //存储已有的事件处理函数(如果已存在一个)
  15. if (element[ "on" +type]){
  16. handlers[ 0 ]=element[ "on" +type];
  17. }
  18. }
  19. //在Hash表中存储该事件处理函数
  20. handlers[handler.$$guid]=handler;
  21. //赋予一个全局事件处理函数来处理所有工作
  22. element[ "on" +type]=handleEvent;
  23. }
  24. };
  25. //创建独立ID的计数器
  26. addEvent.guid= 1 ;
  27. functionremoveEvent(element,type,handler){
  28. if (element.removeEventListener){
  29. element.removeEventListener(type,handler, false );
  30. } else {
  31. //从Hash表中删除事件处理函数
  32. if (element.events&&element.events[type]){
  33. deleteelement.events[type][handler.$$guid];
  34. }
  35. }
  36. };
  37. functionhandleEvent(event){
  38. varreturnValue= true ;
  39. //获取事件处理对象(IE使用全局的事件对象)
  40. event=event||fixEvent((( this .ownerDocument|| this .document|| this ).parentWindow||window).event);
  41. //获取事件处理函数Hash表的引用
  42. varhandlers= this .events[event.type];
  43. //依次处理每个事件处理函数
  44. for (variinhandlers){
  45. this .$$handleEvent=handlers[i];
  46. if ( this .$$handleEvent(event)=== false ){
  47. returnValue= false ;
  48. }
  49. }
  50. return returnValue;
  51. };
  52. //增加一些IE事件对象的缺乏的方法
  53. functionfixEvent(event){
  54. //增加W3C标准事件方法
  55. event.preventDefault=fixEvent.preventDefault;
  56. event.stopPropagation=fixEvent.stopPropagation;
  57. return event;
  58. };
  59. fixEvent.preventDefault=function(){
  60. this .returnValue= false ;
  61. };
  62. fixEvent.stopPropagation=function(){
  63. this .cancelBubble= true ;
  64. };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值