1. 程式人生 > 實用技巧 >事件偵聽函式

事件偵聽函式

const EventUtils = {
    addEnent(element,type,handler){
        if(element.addEventListener){
            element.addEventListener(type.handler,false)
        }else if(element.attachEvent){
            element.attachEvent(`on${type}`,handler)
        }else{
            element[`on${type}`] = handler
        }
    },
    removeEvent(elemnet,type,handler){
        if(element.removeEventListener){
            element.removeEventListener(type.handler,false)
        }else if(element.detachEvent){
            element.detachEvent(`on${type}`,handler)
        }else{
            element[`on${type}`] = null
        }
    },
    getTarget(event){
        return event || window.event
    },
    stopPropagation(event){
        if(event.stopPropagation){
            event.stopPropagation()
        }else{
            event.cancelBubble = true
        }
    },
    preventDefault(event){
        if(event.preventDefault){
            event.preventDefault()
        }else{
            event.returnValue = false
        }
    }   
}