场景:不用每次都对同一个东西进行判断
function addEvent (type, el, fn) {
if (window.addEventListener) { //这样不是很次进来都会对window.addEventListener进行判断
addEvent = function (type, el, fn) {
el.addEventListener(type, fn, false);
}
}
else if(window.attachEvent){
addEvent = function (type, el, fn) {
el.attachEvent('on' + type, fn);
}
}
}
addEvent();
来源:oschina
链接:https://my.oschina.net/u/2285087/blog/3157644