1 /*原文地址:http://ejohn.org/blog/flexible-javascript-events/*/
http://blog.csdn.net/qi1271199790/article/details/53458043
2 /** 3 * [addEvent &&removeEvent description] 4 * @param {[obj]} obj [绑定事件的对象] 5 * @param {str} type [绑定的事件的名称] 6 * @param {Function} fn [事件触发的回调函数] 7 */ 8 var txt=document.getElementById('txt'); 9 function addEvent(obj,type,fn){ 10 if(obj.attachEvent){ 11 obj['e'+type+fn] = fn; 12 obj[type+fn] = function(){ 13 obj['e'+type+fn](window.event); 14 }; 15 obj.attachEvent('on'+type,obj[type+fn]); 16 }else{ 17 obj.addEventListener(type,fn,false); 18 } 19 } 20 function removeEvent(obj,type,fn){ 21 if(obj.detachEvent()){ 22 obj.detachEvent('on'+type,obj[type+fn]); 23 obj[type+fn] = null; 24 }else{ 25 obj.removeEventListener(type,fn,false); 26 } 27 }