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

Posted 鲁小胖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生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
                }
            }
        };

以上是关于原生javascript跨浏览器常用事件处理的主要内容,如果未能解决你的问题,请参考以下文章

javaScirpt事件详解-原生事件基础

跨浏览器的javascript事件的封装

JavaScript 跨浏览器事件处理工具

javaScript跨浏览器事件处理封装代码

JavaScript跨浏览器兼容之事件

js 实现一些跨浏览器的事件方法详解及实例