window.onload = function(){alert(2)} function addEvent (fun) { var old = window.onload; if(typeof old != "function"){ window.onload = fun; }else{ window.onload = function(){ old(); fun(); } } } function delegationforli(){ var oul = document.getElementsByTagName(‘ul‘)[0]; console.log(oul) oul.addEventListener(‘click‘,function(e){ console.log(e.target); if(e.target && e.target.nodeName.toLowerCase() == ‘li‘){ alert(e.target.innerHTML); } }) } addEvent(delegationforli);
用这种window.onload =function() { t(); b(); }方法函数的时候会看着很不爽,可以用上面的方法,更易读。