JS 事件
JS 事件的三个阶段
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>event</title>
<style>
.father {
width: 500px;
height: 500px;
background: #ccc;
}
.son {
width: 200px;
height: 200px;
background: #888;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<script>
document.querySelector('.father').onclick = (e) => {
console.log('Father');
console.log(e.target);
};
document.querySelector('.son').onclick = (e) => {
console.log('Son');
console.log(e.target);
};
document.body.onclick = (e) => {
console.log('Body');
console.log(e.target);
};
document.documentElement.onclick = (e) => {
console.log('Html');
console.log(e.target);
};
</script>
</body>
</html>
阻止冒泡
JS 事件代理

Last updated