跨浏览器事件处理程序

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id='btn'>跨浏览器事件处理程序</button>
  <script>
    let eventUtil = {
      addHandle:function(el,type,handle){
        if(el.addEventListener){
          el.addEventListener(type,handle,false)
        }else if(el.attachEvent){
          el.attachEvent(type,handle,false)
        }else{
          el['on'+type]=handle
        }
      },
      removeHandle:function(el,type,handle){
        if(el.removeEventListener){
          el.removeEventListener(type,handle,false)
        }else if(el.detachEvent){
          el.detachEvent(type,handle,false)
        }else{
          el['on'+type]=null
        }
      }
    }

    // 调用事件工具
    function clickBtn(){
      console.log('点击事件');
    }
    let btn = document.getElementById('btn');
    eventUtil.addHandle(btn,'click',clickBtn)//打印点击事件
    eventUtil.removeHandle(btn,'click',clickBtn)//没有效果
  </script>
</body>
</html>
上一篇:java MD5数据加密工具类


下一篇:一个简单的网址大全1.1.0(C++)