基于“勿重复判断浏览器”的原则,将添加/删除/触发事件的三个分支重写。如下

var w3c = !!window.addEventListener,

	addListener = w3c ?
		function(el, type, fn) { el.addEventListener(type, fn, false); } :
		function(el, type, fn) { el.attachEvent('on' + type, fn); },
		
	removeListener = w3c ?
		function(el, type, fn) { el.removeEventListener(type, fn, false); } :
		function(el, type, fn) { el.detachEvent('on' + type, fn); };
	
	dispatch = w3c ?
		function(el, type){
			try{
				var evt = document.createEvent('Event');
				evt.initEvent(type,true,true);
				el.dispatchEvent(evt);
			}catch(e){alert(e)};
		} :
		function(el, type){
			try{
				el.fireEvent('on'+type);
			}catch(e){alert(e)}
		};

接口改为了on/un/fire

return {
	on : add,
	un : remove,
	fire : dispatch
};

event-0.3.js

相关文章:

  • 2021-11-05
  • 2021-09-09
  • 2022-12-23
  • 2021-08-13
  • 2022-02-05
  • 2021-10-08
猜你喜欢
  • 2021-08-23
  • 2021-10-04
  • 2022-02-11
  • 2021-10-06
  • 2021-12-22
  • 2021-11-23
  • 2021-07-21
相关资源
相似解决方案