事件解绑与解绑的兼容代码

//绑定事件的兼容 function addEventListener(element, type, fn) { if (element.addEventListener) { element.addEventListener(type, fn, false) } else if (element.attachEvent) { element.attachEvent("on" + type, fn); } else { element["on" + type] = fn; } } //解绑事件的兼容 function removeEventListener(element, type, fnName) { if (element.removeEventListener) { element.removeEventListener(type, fnName, false); } else if (element.detachEvent) { element.detachEvent('on' + type, fnName); } else { element["on" + type] = null } } //测试 function f1() { console.log("first"); }function f2() { console.log('second'); }addEventListener(my$('btn1'), 'click', f1); addEventListener(my$('btn1'), 'click', f2); my$('btn2').onclick = function () { removeEventListener(my$('btn1'), 'click', f1) }

    推荐阅读