<!DOCTYPE html>
<html lang="en">
<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>
* {
margin: 0;
padding: 0;
}
.father {
width: 200px;
height: 200px;
background-color: red;
}
.son {
width: 100px;
height: 100px;
background-color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(function () {
/*
1,什么是事件冒泡
事件从下级传递给上级的过程
2,阻止事件冒泡:在下级的函数中添加
return false;
或者
只要触发事件回调函数就会传递一个参数event
拿到event对象,调用event.stopPropagation()
3,什么是默认行为
a标签的跳转网页,使其本身具有的事件性质,这就是默认行为
4.怎么阻止默认行为
1,在下级的函数中添加return false
2, 在下级的函数中添加 event.preventDeafault()
*/
$(".son").click(function (event) {
alert("son");
// return false;
event.stopPropagation()
})
$(".father").click(function () {
alert("father");
})
$("a").click(function (event) {
alert("弹出祖册框");
// return false;
event.preventDeafault()
})
})
</script>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com">我是百度</a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
jQuery中的事件冒泡和默认行为,以及怎么阻止事件冒泡和默认行为
猜你喜欢
转载自blog.csdn.net/weixin_43342105/article/details/104476492
今日推荐
周排行