window.onload 添加多个函数绑定

时间:2021-09-04 02:59:06

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(); }要领函数的时候会看着很不爽,可以用上面的要领,更易读。