js对事件的封装处理

来源:互联网 发布:火鸟移动中文编程官网 编辑:程序博客网 时间:2024/06/06 09:03
Handle.event=(function(){// http://dean.edwards.name/weblog/2005/10/add-event/// a counter used to create unique IDs    var guid = 1;    function _addEvent(element, type, handler) {if (element.addEventListener) {element.addEventListener(type, handler, false);} else {// assign each event handler a unique IDif (!handler.$$guid) handler.$$guid = guid++;// create a hash table of event types for the elementif (!element.events) element.events = {};// create a hash table of event handlers for each element/event pairvar handlers = element.events[type];if (!handlers) {handlers = element.events[type] = {};// store the existing event handler (if there is one)if (element["on" + type]) {handlers[0] = element["on" + type];}}// store the event handler in the hash tablehandlers[handler.$$guid] = handler;// assign a global event handler to do all the workelement["on" + type] = handleEvent;}    }    function _removeEvent(element, type, handler) {        if (element.removeEventListener) {            element.removeEventListener(type, handler, false);        } else {// delete the event handler from the hash table            if (element.events && element.events[type]) {                delete element.events[type][handler.$$guid];            }        }    }    function handleEvent(event){var returnValue = true;// grab the event object (IE uses a global event object)event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);// get a reference to the hash table of event handlersvar handlers = this.events[event.type];// execute each event handlerfor (var i in handlers) {this.$$handleEvent = handlers[i];if (this.$$handleEvent(event) === false) {returnValue = false;}}return returnValue;    }    function fixEvent(event){// add W3C standard event methods        event.preventDefault=fixEvent.preventDefault;        event.stopPropagation=fixEvent.stopPropagation;        return event;    }    fixEvent.preventDefault=function(){        this.returnValue=false;    }    fixEvent.stopPropagation=function(){        this.cancelBubble=true;    }//http://www.thefutureoftheweb.com/blog/adddomloadeventvar load_events = [],load_timer,script,done,exec,init = function () {done = true;clearInterval(load_timer);// kill the timer// execute each function in the stack in the order they were addedwhile (exec = load_events.shift()) exec();if (script) script.onreadystatechange = '';};    return {        domReady:function(func) {// if the init function was already ran, just run this function now and stopif (done||document.body) return func();if (!load_events[0]) {// for Mozilla/Opera9if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);// for Internet Explorerif(K.global.isIE){var proto = "javascript:void(0)";                    if (location.protocol == "https:") proto = "//0";document.write("<script id=__ie_onload defer src="+proto+"><\/scr"+"ipt>");script = document.getElementById("__ie_onload");script.onreadystatechange = function() {    if (this.readyState == "complete") init(); };                    // If IE is used and is not in a frameif(window == top) void function(){     if (done) return;    try {        document.documentElement.doScroll("left");    } catch(e) {         setTimeout(arguments.callee,32);          return;    }    init();}();}// for Safariif (/WebKit/i.test(navigator.userAgent)) { // sniffload_timer = setInterval(function() {if (/loaded|complete/.test(document.readyState)) init(); // call the onload handler}, 10);}// for other browsers set the window.onload, but also execute the old window.onload_addEvent(window,"load",init);}load_events.push(func);},        stopPropagation:function(ev){            var evt = ev||window.event;            if (evt.stopPropagation){             evt.stopPropagation();// MOZ style             }else{            evt.cancelBubble = true;//IE style            }            return false;        },        preventDefault:function(ev){           var evt = ev ||window.event;           if (evt.preventDefault) {       evt.preventDefault();   } else {       evt.returnValue = false;   }        },        addEvent:_addEvent,        removeEvent:_removeEvent    }})();

原创粉丝点击