1 funtion addHangler(target,eventType,Handler) { 2 if(target.addEventListener){ 3 target.addEventListener(eventType,handler,false); 4 }else{ 5 target.attachEvent("on" + eventType , handler); 6 } 7 } 8 9 funtion removeHangler(target,eventType,Handler) { 10 if(target.removeEventListener){ 11 target.removeEventListener(eventType,handler,false); 12 }else{ 13 target.detachEvent("on" + eventType , handler); 14 } 15 }
还有一种预加载技术,适合在全局,在经常用到的场合
1 var addHandler = document.body.addEventListener ? 2 function(target, eventType, handler){ 3 target.addEventListener(eventType, handler, false); 4 }: 5 function(target, eventType, handler){ 6 target.attachEvent("on" + eventType, handler); 7 }; 8 9 10 11 12 var removeHandler = document.body.removeEventListener ? 13 function(target, eventType, handler){ 14 target.removeEventListener(eventType, handler, false); 15 }: 16 function(target, eventType, handler){ 17 target.detachEvent("on" + eventType, handler); 18 };