監聽事件綁定(addEventListener、attachEvent)和移除(removeEventListener、detachEvent)


/** 
* @description 事件綁定,兼容各瀏覽器 
* @param target 事件觸發對象 
* @param type 事件 
* @param func 事件處理函數 
*/ 
function addEvent(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 
}; 

/** 
* @description 事件移除,兼容各瀏覽器 
* @param target 事件觸發對象 
* @param type 事件 
* @param func 事件處理函數 
*/ 
function removeEvent(target, type, func){ 
    if (target.removeEventListener) 
        target.removeEventListener(type, func, false); 
    else if (target.detachEvent) 
        target.detachEvent("on" + type, func); 
    else target["on" + type] = null; 
};  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM