zoukankan      html  css  js  c++  java
  • js 冒泡 捕获

    <!DOCTYPE html>
    <html lang="zh">
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<meta http-equiv="X-UA-Compatible" content="ie=edge">
    	<title></title>
    	<style>
    		.div1{
    			400px;
    			height:400px;
    			background-color: green;
    		}
    		.div2{
    			300px;
    			height:300px;
    			background-color: red;
    		}
    		.div3{
    			200px;
    			height:200px;
    			background-color: blue;
    		}
    				.div4{
    			100px;
    			height:100px;
    			background-color: #CCCCCC;
    		}
    	</style>
    </head>
    <body>
    	<div class="div1 aa">div1
    			<div class="div2 aa">div2
    				<div class="div3 aa">div3
    					<div class="div4 aa">
    					div4
    				</div>
    			</div>
    		</div>
    	</div>
    	<script>
    		var divs=document.querySelectorAll('.aa')
    		//console.log(divs)			
    		for(const div of divs){
    			div.addEventListener("click",function(event){
    				console.log(this.className) //都是默认时冒泡 未阻止 点击 div4时打印 div4 aa div3 aa...div1 aa 若开启阻止则点击div4时只打印div4 aa 
    				event.stopPropagation();//阻止冒泡 捕获  捕获true 未阻止 点击 div4时打印 div1 aa div2 aa...div4 aa 若开启阻止则点击任何div时只打印div1 aa
    			},true)//true是捕获 默认是false冒泡 
    		}
    		
    	</script>
    </body>
    </html>
    

      

  • 相关阅读:
    P1495 曹冲养猪
    luoguP4281[AHOI2008]紧急集合 / 聚会
    NOIP1997 代数表达式
    luogu P3709大爷的字符串题
    luoguP3912 素数个数
    POJ 1065 Wooden Sticks
    POJ 1063 Flip and Shift
    POJ 1062 昂贵的聘礼
    POJ 1003 Hangover
    POJ 1001 Exponentiation
  • 原文地址:https://www.cnblogs.com/howhy/p/11660718.html
Copyright © 2011-2022 走看看