mouseover/mouseout事件, 子元素被移入移出也会触发父元素的事件
mouseenter/mouseleave事件, 子元素被移入移出不会触发父元素的事件(推荐使用)
通过hover监听移入移出(二种方式,jQuery专属的)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>33-jQuery移入移出事件</title>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script src="js/jquery-1.12.4.js"></script>
<script>
$(function () {
// 编写jQuery相关代码
/*
mouseover/mouseout事件, 子元素被移入移出也会触发父元素的事件
*/
/*
$(".father").mouseover(function () {
console.log("father被移入了");
});
$(".father").mouseout(function () {
console.log("father被移出了");
});
*/
/*
mouseenter/mouseleave事件, 子元素被移入移出不会触发父元素的事件
推荐大家使用
*/
/*
$(".father").mouseenter(function () {
console.log("father被移入了");
});
$(".father").mouseleave(function () {
console.log("father被移出了");
});
*/
/*
hover:二个函数监听移入移出
$(".father").hover(function () {
console.log("father被移入了");
},function () {
console.log("father被移出了");
});
*/
hover:一个函数同时监听移入移出
$(".father").hover(function () {
console.log("father被移入移出了");
});
});
</script>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>