标签:
/** * 跨浏览器事件处理工具。只支持冒泡。不支持捕获 * @author (qiu_deqing@126.com) */ var EventUtil = { getEvent: function (event) { return event || window.event; }, getTarget: function (event) { return event.target || event.srcElement; }, // 返回注册成功的监听器,IE中需要使用返回值来移除监听器 on: function (elem, type, handler) { if (elem.addEventListener) { elem.addEventListener(type, handler, false); return handler; } else if (elem.attachEvent) { var wrapper = function () { var event = window.event; event.target = event.srcElement; handler.call(elem, event); }; elem.attachEvent(‘on‘ + type, wrapper); return wrapper; } }, off: function (elem, type, handler) { if (elem.removeEventListener) { elem.removeEventListener(type, handler, false); } else if (elem.detachEvent) { elem.detachEvent(‘on‘ + type, handler); } }, preventDefault: function (event) { if (event.preventDefault) { event.preventDefault(); } else if (‘returnValue‘ in event) { event.returnValue = false; } }, stopPropagation: function (event) { if (event.stopPropagation) { event.stopPropagation(); } else if (‘cancelBubble‘ in event) { event.cancelBubble = true; } }, /** * keypress事件跨浏览器获取输入字符 * 某些浏览器在一些特殊键上也触发keypress,此时返回null **/ getChar: function (event) { if (event.which == null) { return String.fromCharCode(event.keyCode); // IE } else if (event.which != 0 && event.charCode != 0) { return String.fromCharCode(event.which); // the rest } else { return null; // special key } } };
DOM 事件模型是如何的, 编写一个 EventUtil 工具类实现事件管理兼容
参考:
标签:
原文地址:http://www.cnblogs.com/xiaochechang/p/5929318.html