ie8-eventlistener.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //ie8-eventlistener.js
  2. -[1,]||(function(){
  3. //为window对象添加
  4. addEventListener=function(n,f){
  5. if("on"+n in this.constructor.prototype)
  6. this.attachEvent("on"+n,f);
  7. else {
  8. var o=this.customEvents=this.customEvents||{};
  9. n in o?o[n].push(f):(o[n]=[f]);
  10. };
  11. };
  12. removeEventListener=function(n,f){
  13. if("on"+n in this.constructor.prototype)
  14. this.detachEvent("on"+n,f);
  15. else {
  16. var s=this.customEvents&&this.customEvents[n];
  17. if(s)for(var i=0;i<s.length;i++)
  18. if(s[i]==f)return void s.splice(i,1);
  19. };
  20. };
  21. dispatchEvent=function(e){
  22. if("on"+e.type in this.constructor.prototype)
  23. this.fireEvent("on"+e.type,e);
  24. else {
  25. var s=this.customEvents&&this.customEvents[e.type];
  26. if(s)for(var s=s.slice(0),i=0;i<s.length;i++)
  27. s[i].call(this,e);
  28. }
  29. };
  30. //为document对象添加
  31. HTMLDocument.prototype.addEventListener=addEventListener;
  32. HTMLDocument.prototype.removeEventListener=removeEventListener;
  33. HTMLDocument.prototype.dispatchEvent=dispatchEvent;
  34. HTMLDocument.prototype.createEvent=function(){
  35. var e=document.createEventObject();
  36. e.initMouseEvent=function(en){this.type=en;};
  37. e.initEvent=function(en){this.type=en;};
  38. return e;
  39. };
  40. //为全元素添加
  41. var tags=[
  42. "Unknown","UList","Title","TextArea","TableSection","TableRow",
  43. "Table","TableCol","TableCell","TableCaption","Style","Span",
  44. "Select","Script","Param","Paragraph","Option","Object","OList",
  45. "Meta","Marquee","Map","Link","Legend","Label","LI","Input",
  46. "Image","IFrame","Html","Heading","Head","HR","FrameSet",
  47. "Frame","Form","Font","FieldSet","Embed","Div","DList",
  48. "Button","Body","Base","BR","Area","Anchor"
  49. ],html5tags=[
  50. "abbr","article","aside","audio","canvas","datalist","details",
  51. "dialog","eventsource","figure","footer","header","hgroup","mark",
  52. "menu","meter","nav","output","progress","section","time","video"
  53. ],properties={
  54. addEventListener:{value:addEventListener},
  55. removeEventListener:{value:removeEventListener},
  56. dispatchEvent:{value:dispatchEvent}
  57. };
  58. for(var o,n,i=0;o=window["HTML"+tags[i]+"Element"];i++)
  59. tags[i]=o.prototype;
  60. for(i=0;i<html5tags.length;i++)
  61. tags.push(document.createElement(html5tags[i]).constructor.prototype);
  62. for(i=0;o=tags[i];i++)
  63. for(n in properties)Object.defineProperty(o,n,properties[n]);
  64. })();