原生javascript跨浏览器常用事件处理

var eventUntil = {
            getEvent: function (event) {//获取事件
                return event ? event : window.event
            },
            addHandler: function (element, type, handler) {//事件监听
                if (element.addEventListener) {
                    element.addEventListener(type, handler, false);
                } else if (element.attachEvent) {
                    element.attachEvent("on" + type, handler);
                } else {
                    element["on" + type]
                }
            },
            removeHandler: function (event) {//移除事件监听
                if (element.removeEventListener) {
                    element.removeEventListener(type, handler, false);
                } else if (element.detachEvent) {
                    element.detachEvent("on" + type, handler)
                } else {
                    element["on" + type] = null
                }
            },
            stopPropagation: function (event) {//阻止冒泡
                if (event.stopPropagation) {
                    event.stopPropagation();
                } else {
                    event.cancelBubble = true;
                }
            },
            preventDefault: function (event) {//取消事件默认行为
                if (event.preventDefault) {
                    event.preventDefault();
                } else {
                    event.returnValue = false;
                }
            },
            getTarget: function (event) {//获取事件目标
                return event.target || event.srcElement
            },
            getWheelDelta: function (event) {//获取滚轮事件delta增量(opera9.5以下版本wheelDelta值相反)
                if (event.wheelDelta) {
                    return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta)
                } else {
                    return -event.detail * 40//firefox
                }
            }
        };
上一篇:nth-child & nth-of-type区别


下一篇:Java-J2SE学习笔记-查找一个String中,subString的出现次数