兼容event函数

        function evnt(event) {

            var evn = event,
                eventDoc,//当前节点的顶层的 document 对象
                doc,//document.documentElement
                body,
                button = evn.button;//鼠标按键值

            evn.target = evn.target || evn.srcElement;

            // Calculate pageX/Y if missing and clientX/Y available
            if (evn.pageX == null && evn.clientX != null) {
                eventDoc = evn.target.ownerDocument || document;//返回当前节点的顶层的 document 对象
                doc = eventDoc.documentElement || eventDoc.body;

                evn.pageX = evn.clientX + (doc.scrollLeft) - (doc.clientLeft);
                evn.pageY = evn.clientY + (doc.scrollTop) - (doc.clientTop);
            }

            if (!evn.preventDefault) {
                evn.preventDefault = function () {
                    this.returnValue = false;//取消发生事件源元素的默认动作
                }
            }

            if (!evn.stopPropagation) {
                evn.stopPropagation = function () {
                    this.cancelBubble = true;//ie冒泡行为
                }
            }

            if (evn.which == null && (evn.charCode != null || evn.keyCode != null)) {
                evn.which = evn.charCode != null ? evn.charCode : evn.keyCode;
            }

            // Add which for click: 1 === left; 2 === middle; 3 === right
            // Note: button is not normalized, so don't use it
            if (!evn.which && button !== undefined) {
                evn.which = (button & 1 ? 1 : (button & 2 ? 3 : (button & 4 ? 2 : 0)));
            }
            return evn
        };

你可能感兴趣的:(兼容event函数)