码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-06 00:47:01      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:tun   cancel   als   normal   client   asc   ons   mil   默认   

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跨浏览器常用事件处理

标签:tun   cancel   als   normal   client   asc   ons   mil   默认   

原文地址:http://www.cnblogs.com/lpggo/p/7482408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!