日期:2014-05-16 浏览次数:20585 次
//处理浏览器之间的差异 var EventUtil = { addHandler: function (element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler, false); //Dom2级方法 } else if (element.attachEvent) { element.attachEvent("on" + type, handler); //IE方法 } else { element["on" + type] = handler; //Dom0级方法 } }, removeHandler: function (element, type, handler) { if (element.removeEventListener) { element.removeEventListener(type, handler, false); } else if (element.detachEvent) { element.detachEvent("on" + type, handler); } else { element["on" + type] = null; } }, getEvent: function (event) {//返回对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 = false; } }, stopPropagation: function (event) {//阻止事件冒泡 if (event.stopPropagation) { event.stopPropagation(); } else { event.cancelBubble = true; } }, getRelatedTarget: function (event) { if (event.relatedTarget) {//DOM只对于mouseout、mouseover才包含值 return event.relatedTarget; } else if (event.toElement) {//IE mouseout return event.toElement; } else if (event.fromElement) {//IE mouseover return event.fromElement; } else { return null; } }, getButton: function (event) { 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; case 2: //表示按下了次鼠标按钮 case 6: //表示同时按下了次鼠标按钮和中间的鼠标按钮 return 2; case 4: //表示按下了中间的鼠标按钮 return 1; } } }, getWheelDelta: function (event) {//检测事件是否包含wheelDelta属性 if (event.wheelDelta) { return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta); } else { return -event.detail * 40; } }, getCharCode: function (event) {//检测charCode属性是否包含数值 if (typeof event.charCode == "number") { return event.charCode; } else { return event.keyCode; } } }