jQuery移入移出事件
mouseover和mouseout
以下程序一个父div中有一个子div,移入父div打印“father移入”,移出父div打印“father移出”
<script>
$(function()
{
//mouseover
$(".father").on("mouseover",function()
{
console.log("father移入");
})
//mouseout
$(".father").on("mouseout",function()
{
console.log("father移出");
})
}
</script>
<style>
.father{
width: 200px;
height: 200px;
background-color: brown;
}
.son{
width: 100px;
height: 100px;
background-color:cadetblue;
}
.father1{
margin: 10px;
width: 200px;
height: 200px;
background-color: brown;
}
.son1{
width: 100px;
height: 100px;
background-color:cadetblue;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<div class="father1">
<div class="son1"></div>
</div>
</body>
注意! 使用mouseover和mouseout时,只给父级添加这两个事件,移入子级会发生事件捕获,子级会先触发mouseout然后再触发mouseover,此时控制台会打印“father移出”和“father移入”
mouseenter和mouseleave
以下程序一个父div中有一个子div,移入父div打印“father移入”,移出父div打印“father移出”
<script>
$(function()
{
//mouseenter
$(".father1").on("mouseenter",function()
{
console.log("father移入1");
})
//mouseleave
$(".father1").on("mouseleave",function()
{
console.log("father移出1");
})
}
</script>
<style>
.father{
width: 200px;
height: 200px;
background-color: brown;
}
.son{
width: 100px;
height: 100px;
background-color:cadetblue;
}
.father1{
margin: 10px;
width: 200px;
height: 200px;
background-color: brown;
}
.son1{
width: 100px;
height: 100px;
background-color:cadetblue;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<div class="father1">
<div class="son1"></div>
</div>
</body>
!注意这里不会触发事件捕获,移入子div时控制台不会打印“father移出”和“father移入”
hover
- hover(移入函数,移出函数)
- hover(移入移出函数)
- hover和enter、leave一样,不会触发事件捕获