自己收集原生js-2014-2-15

function testforbtn(event){

    alert(window.EventUtil.getEventTarget(window.EventUtil.getEvent( event)));
    //event.target
    //event.type
    //event.currentTarget
    //event.srcElement

}

(function(){})(
    window.EventUtil={
        addHandler:function(element,type,handler){
           // alert(1);
            if(element)
            {
                //alert(2);
                if(element.addEventListener)
                {
                   // alert(3);
                    element.addEventListener(type,handler,false);
                }else if(element.attachEvent)
                {
                   // alert(4);
                    element.attachEvent("on"+type,handler);
                }else{
                   // alert(5);
                    element["on"+type]=handler;
                }

            }
        },
        removeHandler:function(element,type,handler){
            if(element)
            {
                if(element.removeEventListener){
                    element.removeEventListener(type,handler,false);
                }else if(element.detachEvent){
                    element.detachEvent("on"+type,handler);
                }else{
                    element["on"+type]=null;
                }
            }

        },
        addReady: function(fn){
            if(document.addEventListener){//兼容非IE
                document.addEventListener("DOMContentLoaded",function(){
                    //注销事件,避免反复触发
                    document.removeEventListener("DOMContentLoaded",arguments.callee,false);
                    fn();//调用参数函数
                },false);
            }else if(document.attachEvent){//兼容IE
                IEContentLoaded (window, fn);
            }
            function IEContentLoaded (w, fn) {
                var d = w.document, done = false,
                // only fire once
                    init = function () {
                        if (!done) {
                            done = true;
                            fn();
                        }
                    };
                // polling for no errors
                (function () {
                    try {
                        // throws errors until after ondocumentready
                        d.documentElement.doScroll('left');
                    } catch (e) {
                        setTimeout(arguments.callee, 50);
                        return;
                    }
                    // no errors, fire

                    init();
                })();
                // trying to always fire before onload
                d.onreadystatechange = function() {
                    if (d.readyState == 'complete') {
                        d.onreadystatechange = null;
                        init();
                    }
                };
            }
        },
        getEvent:function(event){
            return event?window.event:event;
        },
        getEventTarget: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;
            }

        }

        }
);



window.EventUtil.addReady(
    function(){
        var btn1=document.getElementById("mybtn");

        window.EventUtil.addHandler(btn1,"click",testforbtn);
    }
);
posted @ 2014-02-15 18:13  互联网Fans  阅读(208)  评论(0编辑  收藏  举报