jQuery事件冒泡

jQuery事件冒泡

在这里插入图片描述

一、什么是事件冒泡

冒泡事件就是点击子节点,会向上触发父节点、祖先节点的点击事件。
eg:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.grandfather{
				width: 500px;
				height: 500px;
				background-color: #CCFFCC;
			}
			.father{
				width: 300px;
				height: 300px;
				background-color: #99CCCC;
			}
			.son{
				width: 100px;
				height: 100px;
				background-color: #FFFFCC;
			}
		</style>
		<script type="text/javascript">
			$(function(){
			    var $box1 = $('.grandfather');
			    var $box2 = $('.father');
			    var $box3 = $('.son');
			    $box1.click(function() {
			        alert('grandfather');
			    });
			    $box2.click(function() {
			        alert('father');
			    });
			    $box3.click(function() {
			        alert('son');
			    });
			})
		</script>
	</head>
	<body>
		<div class="grandfather">
			<div class="father">
				<div class="son"></div>
			</div>
		</div>
	</body>
</html>

二、阻止事件冒泡

外层包裹内层的两个元素都被添加事件,此时只想触发一个事件,就需要将另外一个事件进行阻止,这就是阻止事件冒泡。

将上面示例进行修改,点击哪一个div只触发此div
event.stopPropagation(); // 阻止事件冒泡

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.grandfather{
				width: 500px;
				height: 500px;
				background-color: #CCFFCC;
			}
			.father{
				width: 300px;
				height: 300px;
				background-color: #99CCCC;
			}
			.son{
				width: 100px;
				height: 100px;
				background-color: #FFFFCC;
			}
		</style>
		<script type="text/javascript">
			$(function(){
			    var $box1 = $('.grandfather');
			    var $box2 = $('.father');
			    var $box3 = $('.son');
			    $box1.click(function(event) {
			        alert('grandfather');
					event.stopPropagation();
			    });
			    $box2.click(function(event) {
			        alert('father');
					event.stopPropagation();
			    });
			    $box3.click(function(event) {
			        alert('son');
					event.stopPropagation();
			    });
			})
		</script>
	</head>
	<body>
		<div class="grandfather">
			<div class="father">
				<div class="son"></div>
			</div>
		</div>
	</body>
</html>

有时候点击提交按钮会有一些默认事件。比如跳转到别的界面。但是如果没有通过验证的话,就不应该跳转。这时候可以通过设置event.preventDefault(); //阻止默认行为 ( 表单提交 )。
event.preventDefault(); // 阻止事件冒泡

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.father{
				width: 500px;
				height: 500px;
				background-color: red;
			}
			.son{
				width: 300px;
				height: 300px;
				background-color: purple;
			}
			.grandson{
				width: 100px;
				height: 100px;
				background-color: orange;
			}
		</style>
		<script type="text/javascript">
			$(function(){
				$('#form').submit(function(event){
					event.preventDefault();
				})
			})
		</script>
	</head>
	<body>
		<form action="" method="post" id="form">
			<input type="password" name="" />
			<br>
			<input type="submit" value="点击"/>
		</form>
	</body>
</html>

还有一种防止默认行为的方法就是return false。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.father{
				width: 500px;
				height: 500px;
				background-color: red;
			}
			.son{
				width: 300px;
				height: 300px;
				background-color: purple;
			}
			.grandson{
				width: 100px;
				height: 100px;
				background-color: orange;
			}
		</style>
		<script type="text/javascript">
			$(function(){
			     var $box1 = $('.father');
			    var $box2 = $('.son');
			    var $box3 = $('.grandson');
			    $box1.click(function() {
			        alert('father');
			    });
				// 阻止 事件冒泡  event.stopPropagation()
			    $box2.click(function(event) {
			        alert('son');
					return false;
			    });
			    $box3.click(function(event) {
			        alert('grandson');
			    });
			    $(document).click(function(event) {
			        alert('grandfather');
			    }); 
				// 阻止默认行为
				// preventDefault() 这个方法 是通过事件 对象 调用    event
				$('#form').submit(function(event){
					//event.preventDefault();
					return false; //  阻止默认
				})
			})
		</script>
	</head>
	<body>
		<div class="father">
		    <div class="son">
		        <div class="grandson"></div>
		    </div>
		</div>
		
		<form action="" method="post" id="form">
			<input type="password" name="" />
			<br>
			<input type="submit" value="点击"/>
		</form>
	</body>
</html>

总结:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值