DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>事件冒泡title>
<style>
* {
margin: 0;
padding: 0;
}
#out_box {
width: 450px;
height: 450px;
background-color: #929854;
text-align: center;
position: absolute;
top: 100px;
left: 100px;
}
#inner_box {
width: 300px;
height: 300px;
background-color: #4365c3;
line-height: 75px;
position: absolute;
left: 75px;
top: 75px;
}
style>
head>
<body>
<div id="out_box">
<div id="inner_box">李昊哲div>
div>
body>
html>
<script>
let out_box = document.querySelector('#out_box');
out_box.addEventListener('click', function(e) {
alert('out_box');
});
let inner_box = document.querySelector('#inner_box');
inner_box.addEventListener('click', function(e) {
e.cancelBubbl = true;
e.stopPropagation();
e.preventDefault();
alert('inner_box');
});
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62