1.  
  2. var MyEventUtil = function() {
  3. var eventsList = [];
  4. var addEventFn, removeEventFn, getTargetFn, getRelatedTargetFn;
  5. if (document.attachEvent) {
  6. addEventFn = function( obj, type, fn ) {
  7. obj['e'+type+fn] = fn;
  8. obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
  9. obj.attachEvent( 'on'+type, obj[type+fn] );
  10. eventsList.push(arguments);
  11. }
  12. removeEventFn = function ( obj, type, fn ) {
  13. obj.detachEvent( 'on'+type, obj[type+fn] );
  14. obj[type+fn] = null;
  15. }
  16. getTargetFn = function(e) { return e.srcElement; }
  17. getRelatedFn = function(e) { return e.toElement; }
  18. } else if (document.addEventListener) {
  19. addEventFn = function( obj, type, fn ) {
  20. obj.addEventListener( type, fn, false );
  21. eventsList.push(arguments);
  22. }
  23. removeEventFn = function ( obj, type, fn ) {
  24. obj.removeEventListener( type, fn, false );
  25. }
  26. getTargetFn = function(e) { return e.target; }
  27. getRelatedFn = function(e) { return e.relatedTarget; }
  28. } else {
  29. // Not supported!
  30. }
  31.  
  32. return {
  33. getTarget : getTargetFn,
  34. getRelatedTarget : getRelatedFn,
  35. addEvent : addEventFn,
  36. removeEvent : removeEventFn,
  37. flush : function() {
  38. var len = eventsList.length, evt;
  39. for (var i=0;i<len;i++) {
  40. this.removeEvent(eventsList[i][0], eventsList[i][1], eventsList[i][2]);
  41. }
  42. }
  43. }
  44. }();
  45.  
  46.