attachEvent在IE6/7/8下添加同一个类型事件的多个handler执行倒序
如下,
<div id="d1" style="width:200px;height:200px;background:gold;"></div> <script type="text/javascript"> function addEvent(el, type, fn){ if(el.addEventListener){ el.addEventListener(type, fn, false); }else{ el.attachEvent('on' + type, fn); } } var el = document.getElementById('d1'); function handler1(){ alert('test1'); } function handler2(){ alert('test2'); } function handler3(){ alert('test3'); } function handler4(){ alert('test4'); } function handler5(){ alert('test5'); } addEvent(el, 'click', handler1); addEvent(el, 'click', handler2); addEvent(el, 'click', handler3); addEvent(el, 'click', handler4); addEvent(el, 'click', handler5); </script>
以上,给div[id=d1]的click事件添加了5个handler。点击其,结果如下
IE6/7/8 :无序(可多点几次试试)
IE9/Firefox/Safari/Chrome/Opera :test1 -> test2 -> test3 -> test4 -> test5 顺序
点击一下黄色区域试试看