原生js事件绑定和事件移除

[html] view plain copy print ?
  1. /**  
  2.  * @description 事件绑定,兼容各浏览器  
  3.  * @param target 事件触发对象   
  4.  * @param type   事件  
  5.  * @param func   事件处理函数  
  6.  */  
  7. function addEvents(target, type, func) {  
  8.     if (target.addEventListener)    //非ie 和ie9  
  9.         target.addEventListener(type, func, false);  
  10.     else if (target.attachEvent)   //ie6到ie8  
  11.         target.attachEvent("on" + type, func);  
  12.     else target["on" + type] = func;   //ie5  
  13. };  
/**
 * @description 事件绑定,兼容各浏览器
 * @param target 事件触发对象 
 * @param type   事件
 * @param func   事件处理函数
 */
function addEvents(target, type, func) {
    if (target.addEventListener)    //非ie 和ie9
        target.addEventListener(type, func, false);
    else if (target.attachEvent)   //ie6到ie8
        target.attachEvent("on" + type, func);
    else target["on" + type] = func;   //ie5
};

[html] view plain copy print ?
  1. /**  
  2.  * @description 事件移除,兼容各浏览器  
  3.  * @param target 事件触发对象  
  4.  * @param type   事件  
  5.  * @param func   事件处理函数  
  6.  */  
  7. function removeEvents(target, type, func){  
  8.     if (target.removeEventListener)  
  9.         target.removeEventListener(type, func, false);  
  10.     else if (target.detachEvent)  
  11.         target.detachEvent("on" + type, func);  
  12.     else target["on" + type] = null;  
  13. };  

你可能感兴趣的:(jquery)