在使用javascript编程时会遇到一个问题,就是当你给html添加事件时,因为浏览器默认的为冒泡型事件触发机制,因此会触发你不想触发的事件.那么经过以下的函数能够解决这个问题.[兼容IE和FF]javascript
1.阻止事件冒泡,使成为捕获型事件触发机制.css
function stopBubble(e) { //若是提供了事件对象,则这是一个非IE浏览器 if ( e && e.stopPropagation ) //所以它支持W3C的stopPropagation()方法 e.stopPropagation(); else //不然,咱们须要使用IE的方式来取消事件冒泡 window.event.cancelBubble = true; }
2.当按键后,不但愿按键继续传递给如HTML文本框对象时,能够取消返回值.即中止默认事件默认行为(即阻止按键默认行为).html
//阻止浏览器的默认行为 function stopDefault( e ) { //阻止默认浏览器动做(W3C) if ( e && e.preventDefault ) e.preventDefault(); //IE中阻止函数器默认动做的方式 else window.event.returnValue = false; return false; }
那么经过下面的一段代码咱们来看下函数一的效果:java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>效果测试</title> <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function() { //先弹出'单击了div',后弹出'单击了document' $('div.c1').click(function(e){alert('单击了div');}); //只弹出'单击了div' $('div.c2').click(function(e){alert('单击了div');stopBubble(e);}); //单击body,弹出'单击了document' $(document).click(function(e){alert('单击了document');}); $('#txt1').val('123'); //阻止冒泡弹出"单击了document" $('#txt1').click(function(e){stopBubble(e);}); $('#txt1').keydown(function(e){ //取消默认按键事件 stopDefault(e); alert('你按下了键值'+e.keyCode); }); }) function stopBubble(e) { //若是提供了事件对象,则这是一个非IE浏览器 if ( e && e.stopPropagation ) { //所以它支持W3C的stopPropagation()方法 e.stopPropagation(); }else{ //不然,咱们须要使用IE的方式来取消事件冒泡 window.event.cancelBubble = true; } } //阻止浏览器的默认行为 function stopDefault( e ) { //阻止默认浏览器动做(W3C) if ( e && e.preventDefault ) { e.preventDefault(); //IE中阻止函数器默认动做的方式 }else{ window.event.returnValue = false; return false; } } </script> <style type="text/css"> body{ font-size:14px; } } .c1{ font-family:"Arial Unicode MS" } .c2{ font-family:helvetica,simsun,arial,clean } </style> </head> <body> <div class="c1">测试的文字,这里是样式C1,单击以冒泡的形式触发事件.</div><hr/> <div class="c2">测试的文字,这里是样式C2,单击以捕获的形式触发事件.</div><hr/> <div><input id="txt1" name="Text1" type="text" /></div><hr/> </body> </html>
JQuery 提供了两种方式来阻止事件冒泡,Jquery阻止默认动做即通知浏览器不要执行与事件关联的默认动做,下文有个不错的示例,须要的朋友能够参考下jquery
js阻止冒泡
在阻止冒泡的过程当中,W3C和IE采用的不一样的方法,那么咱们必须作如下兼容。
编程
代码以下:浏览器
function stopPro(evt){ var e = evt || window.event; // returnValue若是设置了该属性,它的值比事件句柄的返回值优先级高。把这个属性设置为 fasle, //能够取消发生事件的源元素的默认动做。 // window.event?e.returnValue = false:e.preventDefault(); window.event?e.cancelBubble=true:e.stopPropagation(); }
或者:
函数
代码以下:测试
function cancelBubble(e) { var evt = e ? e : window.event; if (evt.stopPropagation) { //W3C evt.stopPropagation(); }else { //IE evt.cancelBubble = true; }
JQuery 提供了两种方式来阻止事件冒泡。
方式一:event.stopPropagation();
ui
$("#div1").mousedown(function(event){ event.stopPropagation(); });
方式二:return false;
代码以下:
$("#div1").mousedown(function(event){ return false; });
Jquery阻止默认动做即通知浏览器不要执行与事件关联的默认动做。
例如:
代码以下:
$("a").click(function(event){ event.preventDefault(); //阻止默认动做即该连接不会跳转。 alert(4);//可是这个还会弹出 event.stopPropagation();//阻止冒泡事件,上级的单击事件不会被调用 return false;//不只阻止了事件往上冒泡,并且阻止了事件自己 });
可是这两种方式是有区别的。return false 不只阻止了事件往上冒泡,并且阻止了事件自己。event.stopPropagation() 则只阻止事件往上冒泡,不阻止事件自己。
场景应用:Google 和 百度的联想框,当弹出下拉列表,用户在下拉列表区域按下鼠标时须要让光标仍然保持在文本输入框。
Jquery案例1:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>效果测试</title> <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"> </script> <script type="text/javascript"> $(function(){ //父元素绑定单击事件 $("#ee").click(function(){ alert("ee"); }); //子元素绑定单击事件,会弹出"aa"和3 $("#aa").click(function(event){ alert("aa"); event.preventDefault(); //阻止默认行为 event.stopPropagation(); //阻止向父页面冒泡 alert(3); }); //子元素绑定单击事件,会弹出4,不会跳转 $("a").click(function(event){ event.preventDefault(); //阻止超连接a的默认行为---"跳转" alert(4); event.stopPropagation(); //阻止向父页面冒泡 return false; }); }); </script> </head> <body> <div id="ee" style="border: 1px solid"> 我是div的内容 <input id="aa" type="button" value="test" /> <a href="http://baidu.com">我是指向www.baidu.com的超连接</a> </div> </body> </html>
案例2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>效果测试</title> <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"> </script> <script type="text/javascript"> function tt(){ alert("div"); } function ttt(){ //在全部浏览器中获取event对象 var e = arguments.callee.caller.arguments[0] || window.event; //阻止默认事件"跳转",若是不这么写,那么会在弹出"3","4"以后,跳转到"百度"页面 window.event?e.returnValue = false:e.preventDefault(); alert(3); //阻止向父元素冒泡 window.event?e.cancelBubble = true:e.stopPropagation(); alert(4); } </script> </head> <body> <div onclick = "tt();" style="border: 1px solid"> 我是div的内容 <a href="http://baidu.com" onclick="ttt();">我是指向"www.baidu.com"的超连接</a> </div> </body> </html>