zoukankan      html  css  js  c++  java
  • JavaScript-跨浏览器事件处理程序(EventUtil)

    事件操作对象:

    var EventUtil= {

      //添加事件

      addHandler: function (element, type, handler) {
        if (element.addEventListener) {
          element.addEventListener(type, handler, false);
        }else if (element.attachEvent) {  //IE
          element.attachEvent("on" + type, handler, false);
        } else {
          element["on" + type] = handler;
        }
      },

      //移除对象
      removeHandler: function (element, type, handler) {
        if (element.removeEventListener) {
          element.removeEventListener(type, handler, false);
        } else if (element.detachEvent) {  //IE
          element.detachEvent("on" + type, handler, false);
        } else {
          element["on" + type] = null;
        }
       },

      //获取事件对象
      getEvent: function (event) {
        return event ? event : window.event;
      },

      //获取目标对象
      getTarget: function (event) {
        return event.target || event.srcElement;
      },

      //取消默认事件
      preventDefault: function (event) {
        if (event.preventDefault) {
          event.preventDefault();
        } else {  
          event.returnValue;
        }
      },

      //停止冒泡

      stopPropagation: function (event) {
        if (event.stopPropagation) {
          event.stopPropagation();
        } else {

          event.cancelBubble = true;
        }
      }
    };

  • 相关阅读:
    2016.04.18 view中嵌套两个居中的 lable
    2016..4.06 点菜系统案例:利用文本框的inputView和inputAccessoryView串联UIPickerView、UIDateP...
    2016.04.06 UITabBar+badge.h
    2014 .04 .06 navbar
    2016-03-17 leaks 内存泄露
    2016-03-16 sdwebimage
    2016-03-16 字符串的包含
    2016-03-16 键盘的弹出与收起
    2016-03-15 nsfoundation 数组 字典 nsrange等
    2016-03-15 uitextview placehold
  • 原文地址:https://www.cnblogs.com/dongshenjun/p/13471967.html
Copyright © 2011-2022 走看看