Javascript中对EventUtil的封装

//
//封装EventUtil对象

var EventUtil = {

//
//Javascript高级程序设计一书中对EventUtil的封装,分别在DOM2级、IE HTML、DOM0级对事件处理对象作了封装

    addHandler: function (element, type, handler) {
//
//DOM2
        if (element.addEventListener) {
            element.addEventListener(type, handler, false);
        }

//
//IE
        else if (element.attachEvent) {
            element.attachEvent("on" + type, handler, false);
        }

//
//DOM0
        else {
            element["on" + type] = 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["on" + type] = null;
        }
    },

    getEvent: function (event) {
        return event ? event : window.event;
    },

    getTarget: function (event) {
        return event.target || event.srcElement;
    },

    preventDefault: function (event) {
        if (event.preventDefault) {
            event.preventDefault();
        }
        else {
            event.returnValue = false;
        }
    },

    stopPropagation: function (event) {
        if (event.stopPropagation) {
            event.stopPropagation();
        }
        else {
            event.cancelBubble = true;
        }
    }
};


你可能感兴趣的:(javascript,function,html,null,ie)