---
title: "js总结"
author: "chokee"
date: "2013-10-11T10:38:14Z"
tags: []
categories: ["开源软件"]
slug: "js"
draft: false
lastmod: "2013-10-11T12:10:50Z"
id: 1954728
url: "https://chokee.iteye.com/blog/1954728"
---

![](http://dl2.iteye.com/upload/attachment/0090/0520/3d45e922-a2a5-3fde-a2ec-20d3d9c96aea.png)

![](http://dl2.iteye.com/upload/attachment/0090/0522/799a4cf4-1e36-33dc-a360-38a32172b34c.png)

![](http://dl2.iteye.com/upload/attachment/0090/0524/e6f76f8c-34e9-39d2-8510-18f35480ad10.png)

![](http://dl2.iteye.com/upload/attachment/0090/0546/177eb768-07ce-3912-af3c-8ae7c2cfa270.png)

![](http://dl2.iteye.com/upload/attachment/0090/0548/37b5d5bd-a7be-31bd-9613-b4ec4c28e86e.png)

![](http://dl2.iteye.com/upload/attachment/0090/0550/d3e316a4-b162-3513-8b05-d6829f7b53d4.png)

![](http://dl2.iteye.com/upload/attachment/0090/0586/27981536-3c6d-38ff-9d28-04ef77619193.png)

![](http://dl2.iteye.com/upload/attachment/0090/0588/0c0dc6f4-7448-3e1f-86bd-b45996711f22.png)

var EventUtil = {
    //注册事件
    addHandler: function(element, type, handler){
        if (element.addEventListener){
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent){
            element.attachEvent("on" + type, handler);
        } else {
            element = handler;
        }
    },
 
    //移除事件
    removeHandler: function(element, type, handler){
        if (element.removeEventListener){
            element.removeEventListener(type, handler, false);
        } else if (element.detachEvent){
            element.detachEvent("on" + type, handler);
        } else {
            element = null;
        }
    },
 
    //获取事件
    getEvent: function(event){
        return event ? event : window.event;
    },
 
    //获取事件名称
    getTarget: function(event){
        return event.target || event.srcElement;
    },
    
    //获取鼠标键
    getButton: function(event){
        //DOM2中检测是否有MouseEvents模块
        if (document.implementation.hasFeature("MouseEvents", "2.0")){
            return event.button;
            // var k = event.button;
            //     switch(k){
            //         case 0:
            //             return "0:表示左键";
            //         case 1:
            //             return "1:表示中键";
            //         case 2:
            //             return "2:表示右键";
            //     }
            //IE6,7,8  左键:1,中键:4,右键:2;
            //Chrome,FF,IE9+   左键:0,中键:1,右键:2;
        } else {
            switch(event.button){   //IE下
                case 0:
                case 1:             //左键
                case 3:             //左右键
                case 5:             //左中键
                case 7:             //左右中
                    return 0;       //左键
                case 2:             //右键
                case 6:             //右中
                    return 2;       //右键
                case 4: return 1;   //中键
            }
        }
    },
    
    //获得按键编码值
    getCharCode: function(event){
        if (typeof event.charCode == "number"){
            return event.charCode;
        } else {
            return event.keyCode;
        }
    },
    
    /*event对象的relatedTarget属性提供了相关元素的信息，这个属性只对于mouseover和mouseout事件才包含值；
    *对于其他事件，这个属性的值是null。
    *IE不支持relatedTarget属性，但提供了保存着同样信息的不同属性。
    *在mouseover事件触发时，IE的fromElement属性中保存率相关元素；
    *在mouseout事件触发时，IE的toElement属性中保存着相关元素。
    */
    getRelatedTarget: function(event){
        if (event.relatedTarget){
            return event.relatedTarget;
        } else if (event.toElement){
            return event.toElement;
        } else if (event.fromElement){
            return event.fromElement;
        } else {
            return null;
        }
    
    },
    
    //获取鼠标滚轮mousewheel事件
    getWheelDelta: function(event){
        if (event.wheelDelta){
            return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta);
        } else {
            return -event.detail * 40;
        }
    },
    
    //取消事件的默认行为，如果cancelable是true，则可以使用这个方法；
    //如:点击超链接时阻止其访问herf属性实现跳转
    preventDefault: function(event){
        if (event.preventDefault){
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    },
    
    //取消事件的进一步捕获或冒泡。如果bubbles为true则可以使用这个方法；
    stopPropagation: function(event){
        if (event.stopPropagation){
            event.stopPropagation();
        } else {
            event.cancelBubble = true;
        }
    },
 
    //获取剪切板文本
    getClipboardText: function(event){
        var clipboardData =  (event.clipboardData || window.clipboardData);
        return clipboardData.getData("text");
    },
 
    //设置剪切板文本
    setClipboardText: function(event, value){
        if (event.clipboardData){
            event.clipboardData.setData("text/plain", value);
        } else if (window.clipboardData){
            window.clipboardData.setData("text", value);
        }
    }
 
};


![](http://dl2.iteye.com/upload/attachment/0090/0594/dee015c2-821b-3ea3-aa3d-842defaeae82.png)