文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 35 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 387354 人次
  • RSS订阅: 文章|评论

小巧独立的Javascript库(1):Events.js

KY8 于 2009-03-01 16:07:11 发表JavaScript

Dean Edwards实现的Javascript Events管理:http://dean.edwards.name/weblog/2005/10/add-event/。特性:

  • 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
  1. // written by Dean Edwards, 2005 
  2. // http://dean.edwards.name/ 
  3.  
  4. function addEvent(element, type, handler) { 
  5.   // assign each event handler a unique ID 
  6.   if (!handler.$guid) handler.$guid = addEvent.guid++; 
  7.   // create a hash table of event types for the element 
  8.   if (!element.events) element.events = {}; 
  9.   // create a hash table of event handlers for each element/event pair 
  10.   var handlers = element.events[type]; 
  11.   if (!handlers) { 
  12.     handlers = element.events[type] = {}; 
  13.     // store the existing event handler (if there is one) 
  14.     if (element["on" + type]) { 
  15.       handlers[0] = element["on" + type]; 
  16.     } 
  17.   } 
  18.   // store the event handler in the hash table 
  19.   handlers[handler.$guid] = handler; 
  20.   // assign a global event handler to do all the work 
  21.   element["on" + type] = handleEvent; 
  22. }; 
  23. // a counter used to create unique IDs 
  24. addEvent.guid = 1; 
  25.  
  26. function removeEvent(element, type, handler) { 
  27.   // delete the event handler from the hash table 
  28.   if (element.events && element.events[type]) { 
  29.     delete element.events[type][handler.$guid]; 
  30.   } 
  31. }; 
  32.  
  33. function handleEvent(event) { 
  34.   // grab the event object (IE uses a global event object) 
  35.   event = event || window.event; 
  36.   // get a reference to the hash table of event handlers 
  37.   var handlers = this.events[event.type]; 
  38.   // execute each event handler 
  39.   for (var i in handlers) { 
  40.     this.$handleEvent = handlers[i]; 
  41.     this.$handleEvent(event); 
  42.   } 
  43. }; 

下面是一个改进版(防止内存泄漏):
 

  1. function addEvent(element, type, handler) { 
  2.     // assign each event handler a unique ID 
  3.     if (!handler.$guid) handler.$guid = addEvent.guid++; 
  4.   // assign each element a unique ID 
  5.   if (!element.$guid) element.$guid = addEvent.guid++; 
  6.     // create a hash table of event types for the element 
  7.   if (!addEvent.handlers[element.$guid]) addEvent.handlers[element.$guid] = {}; 
  8.     // create a hash table of event handlers for each element/event pair 
  9.     var handlers = addEvent.handlers[element.$guid][type]; 
  10.     if (!handlers) { 
  11.         handlers = addEvent.handlers[element.$guid][type] = {}; 
  12.         // store the existing event handler (if there is one) 
  13.         if (element["on" + type]) { 
  14.             handlers[0] = element["on" + type]; 
  15.         } 
  16.     } 
  17.     // store the event handler in the hash table 
  18.     handlers[handler.$guid] = handler; 
  19.     // assign a global event handler to do all the work 
  20.     element["on" + type] = handleEvent; 
  21. }; 
  22. // a counter used to create unique IDs 
  23. addEvent.guid = 1; 
  24. // a global hash table containing all handlers 
  25. addEvent.handlers = {}; 
  26.  
  27. function removeEvent(element, type, handler) { 
  28.   // check if the element has a guid 
  29.   if (!element.$guid) return
  30.     // delete the event handler from the hash table 
  31.     if (addEvent.handlers[element.$guid] && addEvent.handlers[element.$guid][type]) { 
  32.         delete addEvent.handlers[element.$guid][type][handler.$guid]; 
  33.     } 
  34. }; 
  35. function handleEvent(event) { 
  36.     // grab the event object (IE uses a global event object) 
  37.     event = event || window.event; 
  38.     // get a reference to the hash table of event handlers 
  39.     var handlers = addEvent.handlers[this.$guid][event.type]; 
  40.     // execute each event handler 
  41.     for (var i in handlers) { 
  42.         this.$handleEvent = handlers[i]; 
  43.         this.$handleEvent(event); 
  44.     } 
  45. }; 
被阅659次, 1票event 发表评论
1 / 1 / 1 | « 1 » |
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号