Home >Web Front-end >JS Tutorial >js event handler cross-browser solution_javascript skills

js event handler cross-browser solution_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:08:03968browse

The example in this article shares the js event handler cross-browser solution for your reference. The specific content is as follows

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div>
  <input type="button" id="button1" value="按钮" />
</div>
<script type="text/javascript">
  function show(){
    alert("Hello world!");
  }
  //声明一个对象
  var eventUtil={
  //添加句柄
    addHandler:function(element,type,handler){
  //DOM2级事件处理判断
    if(element.addEventListener){
      element.addEventListener(type,handler,false);
    }else if(element.attachEvent){//IE事件处理判断
      element.attachEvent('on'+type,handler);
    }else{//DOM0级事件判断
      element['on'+type]=handler;
    }
  },
  //删除句柄
  removeHandler:function(element,type,handler){
  //DOM2级事件处理判断
  if(element.removeEventListener){
    elememt.removeEventListener(type,handler,false);
  }else if(element.detachEvent){//IE事件处理判断
    element.detachEvent('on'+type,handler);
  }else{//DOM0级事件判断
    element['on'+type]=null;
    }
  }
}
  eventUtil.addHandler(button3,'click',show);
</script>

</body>
</html>

I will share with you the cross-browser code for js native event processing:

//跨浏览器的事件处理器添加方式
var EventUtil = {
  addHandler : function(elem, type, handler){
    if(elem.addEventListener){
      elem.addEventListener(type, handler, false);
    }
    else if(elem.attachEvent){
      elem.attachEvent("on" + type, handler);//添加多个同一类型的handler时,IE方式的规则是最后添加的最先触发
    }
    else{
      if(typeof elem["on" + type] === 'function'){
        var oldHandler = elem["on" + type];
        elem["on" + type] = function(){
          oldHandler();
          handler();
        }
      }
      else{
        elem["on" + type] = handler;//支持添加多个事件处理器
      }
    }
  },

  getEvent : function(event){
    return event &#63; event : window.event;
  },

  getTarget : function(event){
    return event.target || event.srcElement;
  },

  preventDefault : function(event){
    if(event.preventDefault){
      event.preventDefault();
    }
    else{
      event.returnValue = false;
    }
  },

  removeHandler : function(elem, type, handler){
    if(elem.removeEventListener){
      elem.removeEventListener(type, handler, false);
    }
    else if(elem.detachEvent){
      elem.detachEvent("on" + type, handler);
    }
    else{
      elem["on" + type] = null;//不支持移除单一事件处理器,只能全部移除
    }
  },

  stopPropagation : function(event){
    if(event.stopPropagation){
      event.stopPropagation();
    }
    else{
      event.cancelBubble = true;
    }
  },

  getRelatedTarget : function(event){
    if(event.relatedTarget){
      return event.relatedTarget;
    }
    else if(event.toElement && event.type == "mouseout"){
      return event.toElement;
    }
    else if(event.fromElement && event.type == "mouseover"){
      return event.fromElement;
    }
    else{
      return null;
    }
  },

  /*IE8点击左键和中键都是0;FF无法识别中键;Chrome正常*/
  getButton : function(event){//返回0,1,2 - 左,中,右
    if(document.implementation.hasFeature("MouseEvents", "2.0")){
      return event.button;
    }
    else{
      switch(event.button){
        case 0:case 1:case 3:case 5:case 7:
          return 0;
          break;
        case 2:case 6:
          return 2;
          break;
        case 4:
          return 1;
          break;
        default:
          break;
      }
    }
  },

  /*只能检测keypress事件,返回值等于将要显示的字符编码*/
  /*IE和Chrome只有能显示的字符键才触发,FF其它键也能触发,返回值为0*/
  getCharCode : function(event){
    if(typeof event.charCode == "number"){
      return event.charCode;
    }
    else{
      return event.keyCode;
    }
  }
};

The above is the entire content of this article. I hope it will be helpful to everyone in solving the problem of js event handler cross-browser.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn