学习了jQuery的移入移除事件。
1.mouseover/mouseout事件,子元素被移入移除也会触发父元素事件
2.mouseenter/mouseleave事件,子元素被移入移除不会触发父元素事件,建议大家使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>33-jQuery移入移除事件</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background:red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script src="JS_file/jquery-1.12.4.js"></script>
<script>
$(function () {
// 编写jQuery相关代码
/*
mouseover/mouseout事件,子元素被移入移除也会触发父元素事件
* */
/* $(".father").mouseover(function () {
console.log("father被移入了");
});
$(".father").mouseout(function () {
console.log("fathre被移出了");
});*/
/*
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>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>