currentTarget和target的区别

182 阅读1分钟
<!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>Document</title>
	<style>
	.father{
		width: 200px;
		height: 200px;
		background-color: blue;
	}
	.son{
		width: 100px;
		height: 100px;
		background-color: red;
	}
	</style>
</head>
<body>
	<div class="father">
		<div class="son"></div>
	</div>
</body>
<script>
	let father = document.querySelector('.father')
	let son = document.querySelector('.son')

	son.addEventListener('click', function(event){
		console.log(event.currentTarget)
	})

	father.addEventListener('click', function(event){
		console.log(event.currentTarget)
	})
</script>
</html>

点击class为son的div,运行结果如下: currenttarget.png

现在把currentTarget改为target,运行结果如下: target.png

总结

  • currentTarget 为 事件绑定的元素
  • target 为 触发事件的元素