jQery鼠标移入移出事件

本文详细探讨了jQuery中的鼠标移入(mouseenter)和移出(mouseleave)事件,区别于普通的hover事件,它们如何更精确地控制元素交互。通过实例代码,展示了如何使用这些事件来实现更流畅的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标移入移出事件</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 200px;
            height: 200px;
            background: red;
        }
        .son{
            width: 100px;
            height: 100px;
            background: blue;
        }
    </style>
    <script src="../jquery-3.3.1.js"></script>
    <script>
        $(function () {
            // $(".father").mouseover(function(){
            //       alert("father被移入了"); 
            // });
            // $(".father").mouseout(function(){
            //       console.log("father被移出了"); 
            // });
            //mouseover mouseout子元素会触发父元素事件


            //mouseenter mouseleav  子元素不会触发父元素事件
            // $(".father").mouseenter(function(){
            //       console.log("father被移入了"); 
            // });
            // $(".father").mouseleave(function(){
            //       console.log("father被移出了"); 
            // });


             $(".father").hover(function(){
                  console.log("father被移入了"); 
            },function(){
                console.log("father被移出了"); 
            });
        });
    </script>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值