Home > Web Front-end > JS Tutorial > An introduction to how javascript prevents event bubbling and browser default behavior

An introduction to how javascript prevents event bubbling and browser default behavior

巴扎黑
Release: 2017-07-18 18:07:34
Original
1838 people have browsed it

1. Prevent event bubbling and make it a capture-type event triggering mechanism.

1 function stopBubble(e) { 
2 //如果提供了事件对象,则这是一个非IE浏览器 3 if ( e && e.stopPropagation ) 
4     //因此它支持W3C的stopPropagation()方法 5     e.stopPropagation(); 
6 else7     //否则,我们需要使用IE的方式来取消事件冒泡 8     window.event.cancelBubble = true; 
9 }
Copy after login

2. After pressing a key, you do not want the key to continue to be passed to an object such as an HTML text box. You can cancel the return value. That is, stop the default behavior of the default event.

 1 //阻止浏览器的默认行为  2 function stopDefault( e ) { 
 3     //阻止默认浏览器动作(W3C)  4     if ( e && e.preventDefault ) 
 5         e.preventDefault(); 
 6     //IE中阻止函数器默认动作的方式  7     else 8         window.event.returnValue = false; 
 9     return false; 
10 }
Copy after login

Then let’s look at the effect of function one through the following piece of code:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3   4 <head> 5 <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 6 <title>效果测试</title> 7 <script language="javascript" type="text/javascript" src="jquery-1.4.2.js?1.1.11"></script> 8 <script language="javascript" type="text/javascript"> 9 $(document).ready(function()10 {11 $('div.c1').click(function(e){alert('单击了div');});12 $('div.c2').click(function(e){alert('单击了div');stopBubble(e);});13 $(document).click(function(e){alert('单击了document');});14 $('#txt1').val('123');15 $('#txt1').click(function(e){stopBubble(e);});16 $('#txt1').keydown(function(e){stopDefault(e);alert('你按下了键值'+e.keyCode); });17 })18  19 function stopBubble(e) { 
20 //如果提供了事件对象,则这是一个非IE浏览器 21     if ( e && e.stopPropagation ) 
22     //因此它支持W3C的stopPropagation()方法 23     e.stopPropagation(); 
24      else 25     //否则,我们需要使用IE的方式来取消事件冒泡 26     window.event.cancelBubble = true; 
27 } 
28 //阻止浏览器的默认行为 29 function stopDefault( e ) { 
30     //阻止默认浏览器动作(W3C) 31     if ( e && e.preventDefault ) 
32         e.preventDefault(); 
33     //IE中阻止函数器默认动作的方式 34     else 35         window.event.returnValue = false; 
36     return false; 
37 }38 </script>39 <style type="text/css">40 body{41 font-size:14px;42     }43 }44 .c1{45     font-family:"Arial Unicode MS"46     }47 .c2{48     font-family:helvetica,simsun,arial,clean49     }50 </style>51 </head>52  53 <body>54  55 <div class="c1">测试的文字,这里是样式C1,单击以冒泡的形式触发事件.</div><hr/>56  57 <div class="c2">测试的文字,这里是样式C2,单击以捕获的形式触发事件.</div><hr/>58  59 <div><input id="txt1" name="Text1" type="text" /></div><hr/>60  61 </body>62 </html>
Copy after login

General method to stop bubbling:

function stopBubble(e) {
//如果提供了事件对象,是非IE浏览器
if ( e && e.stopPropagation )
  //使用W3C的stopPropagation()方法
  e.stopPropagation();
else
  //使用IE的cancelBubble = true来取消事件冒泡
  window.event.cancelBubble = true;
}
Copy after login

Block the browser Default behavior-general method

//Block the browser’s default behavior

function stopDefault( e ) {
  //阻止默认浏览器动作(W3C)
  if ( e && e.preventDefault )
    e.preventDefault();
  //IE中阻止函数器默认动作的方式
  else
    window.event.returnValue = false;
  return false;
}
Copy after login


The above is the detailed content of An introduction to how javascript prevents event bubbling and browser default behavior. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template