Heim >Web-Frontend >js-Tutorial >阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)_javascript技巧

阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)_javascript技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 19:13:451091Durchsuche

cancelBubble在IE下有效
stopPropagation在Firefox下有效

复制代码 代码如下:

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
 
 阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation) 
 
<script> <BR>function doSomething (obj,evt) { <BR> alert(obj.id); <BR> var e=(evt)?evt:window.event; <BR> if (window.event) { <BR> e.cancelBubble=true; <BR> } else { <BR> //e.preventDefault(); <BR> e.stopPropagation(); <BR> } <BR>} <BR></script> 
 
 
 
 

This is parent1 div.

 
 
 
 

This is child1.

 
 
 
 

This is parent1 div.

 
 

 
 
 

This is parent2 div.

 
 
 
 

This is child2. Will bubble.

 
 
 
 

This is parent2 div.

 
 
 
 
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn