导航

javascript-回归原生基础

Posted on 2018-04-26 10:03  小飞博客  阅读(141)  评论(0编辑  收藏  举报
 1 //添加事件监听兼容函数  
 2 function addHandler(target, eventType, handler){  
 3     if(target.addEventListener){//主流浏览器  
 4         addHandler = function(target, eventType, handler){  
 5             target.addEventListener(eventType, handler, false);  
 6         };  
 7     }else{//IE  
 8         addHandler = function(target, eventType, handler){  
 9             target.attachEvent("on"+eventType, handler);  
10         };        
11     }  
12     //执行新的函数  
13     addHandler(target, eventType, handler);  
14 }  
15 //删除事件监听兼容函数  
16 function removeHandler(target, eventType, handler){  
17     if(target.removeEventListener){//主流浏览器  
18         removeHandler = function(target, eventType, handler){  
19             target.removeEventListener(eventType, handler, false);  
20         }         
21     }else{//IE  
22         removeHandler = function(target, eventType, handler){  
23             target.detachEvent("on"+eventType, handler);  
24         }         
25     }  
26     //执行新的函数  
27     removeHandler(target, eventType, handler);  
28 }