js对事件的封装处理

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 ID
			if (!handler.$$guid) handler.$$guid = guid++;
			// create a hash table of event types for the element
			if (!element.events) element.events = {};
			// create a hash table of event handlers for each element/event pair
			var 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 table
			handlers[handler.$$guid] = handler;
			// assign a global event handler to do all the work
			element["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 handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (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/adddomloadevent
	var 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 added
			while (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 stop
			if (done||document.body) return func();
			if (!load_events[0]) {
				// for Mozilla/Opera9
				if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);

				// for Internet Explorer
				if(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 frame
					if(window == top) void function(){ 
					    if (done) return;
					    try {
					        document.documentElement.doScroll("left");
					    } catch(e) { 
					        setTimeout(arguments.callee,32);  
					        return;
					    }
					    init();
					}();
				}

				// for Safari
				if (/WebKit/i.test(navigator.userAgent)) { // sniff
					load_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
    }
})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值