Web开发基础_JQuery学习_0005_jQuery冒泡机制

jQuery事件冒泡机制

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>冒泡机制</title>
		<style type="text/css">
			div {
				width:300px;
				padding:30px;
				border:1px solid red;
			}
			
			p {
				width:200px;
				padding:20px;
				border:1px solid red;
			}
			
			span {
				border:1px solid red;
			}
		</style>
		<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
		<script type="text/javascript">
			//onload
			$(function(){
				$("div").click(function(e){
					alert("div");
					//获取事件源
					console.log(e.target);
				});
				
				$("p").click(function(){
					alert("p");
				});
				
				$("span").click(function(e){
					alert("span");
					//取消冒泡
					e.stopPropagation();
				});
			});
		</script>
	</head>
	<body>
		<div>
			<p>
				<span>xxx</span>
			</p>
		</div>
	
	</body>
</html>

最终页面显示效果:

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/81431612