跨浏览器事件对象

    var EventUtil = {
        addHandler: function(elem,type,handler){
            if (elem.addEventListener) {
                    elem.addEventListener(type, handler, false);
            }else if(elem.attachEvent){
                    elem.attachEvent('on'+type,handler);
            }else{
                    elem["on"+type] = handler;
            }
        },
        removeHandler: function(elem,type,handler){
            if (elem.removeEventListener) {
                    elem.removeEventListener(type, handler, false);
            }else if(elem.attachEvent){
                    elem.detachEvent('on'+type,handler);
            }else{
                    elem["on"+type] = handler;
            }
        },
        getEvent: function(event){
                return event ? event : window.event;
        },
        getTarget: function(event){
                return event.target || event.srcElement;
        },
        preventDefault: function(e){
                if ( e && e.preventDefault ) 
                    e.preventDefault(); 
                else 
                    window.event.returnValue = false; 

                return false; 
        }
    };

EventUtil.addHandler(document.getElementById('iphone'),'click',function(e){


		alert("iPhone版暂无下载,稍后推出,程序员正在拼命加班!");

        EventUtil.preventDefault(e);


});
posted @ 2014-07-17 10:01  唸随爱  阅读(155)  评论(0编辑  收藏  举报