mouseenter 鼠標事件
- 當鼠標移動到元素上時就會觸發 mouseenter 事件
- 類似 mouseover,它們兩者事件的差別是
- mouseover 鼠標經過自身盒子會觸發,經過子盒子還會觸發,mouseenter 只會經過自身盒子觸發
- 之所以這樣,就是因爲mouseenter不會冒泡
- 跟 mouseenter 搭配鼠標離開 mouseleave 同樣不會冒泡
栗子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father {
width: 200px;
height: 200px;
background-color: pink;
}
.son {
width: 100px;
height: 100px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<script>
var father = document.querySelector('.father');
var son = document.querySelector('son');
// father.addEventListener('mouseover', function() {
// console.log(11);
// })
father.addEventListener('mouseenter', function() {
console.log(11);
})
</script>
</body>
</html>