事件侦听函数

const EventUtils = {
    addEvent(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
        }
    }   
}

#兼容版#

posted @ 2020-07-24 21:23  671_MrSix  阅读(111)  评论(0编辑  收藏  举报