日期:2014-05-16 浏览次数:20341 次
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script> <script type="text/javascript"> function click_address_line(){ $("address_line").addEvent("click", function (e){ stopBubble(e); var target=e.target; if(target.id=="address_edit" || target.getParent().id=="address_edit" ){ alert(1); // return; //是不是这样做的? } alert(2); //怎样不执行这冒泡事件 }); } window.addEvent("domready",function(){ click_address_line(); }); function stopBubble(e){ //如果提供了事件对象,则这是一个非IE浏览器 if (e && e.stopPropagation ){ e.stopPropagation(); } else{ //否则,我们需要使用IE的方式来取消事件冒泡 window.event.cancelBubble = true; } } </script> <div id="address_line"> <a href="" id="address_edit">address_edit</a> </div>
<!DOCTYPE html> <html> <head> <style type="text/css"> </style> <meta charset="UTF-8" /> </head> <body> <div id="div1"> <a href="#" id="a1">事件会冒泡了</a> </div> <div id="div2"> <a href="#" id="a2">事件不会冒泡</a> </div> </body> </html> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $('div').click(function(){ alert('事件冒泡了'); }); $('a').click(function(e){ if(e.target.id == 'a2'){ e.stopPropagation(); } }) </script>