鼠标划过/划出事件

一、hover

<!DOCTYPE html>
<html>
<body>
    <div class="box" style="background-color:green;width:120px;height:20px;padding:40px;color:#ffffff;">把鼠标移到上面</div>
    <script src="js/jquery-1.8.3.min.js"></script>
    <script>
        $('.box').hover(function() {
            $('.box').addClass('over');
        }, function() {
            $('.box').removeClass('over').addClass('out');
        });
    </script>
</body>
</html>

二、第一种写法 onmouseover onmouseout

<!DOCTYPE html>
<html>
<body>
    <div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:green;width:120px;height:20px;padding:40px;color:#ffffff;">把鼠标移到上面</div>
    <script>
        function mOver(obj) {
            obj.innerHTML = "谢谢"
        }

        function mOut(obj) {
            obj.innerHTML = "把鼠标移到上面"
        }
        
    </script>
</body>
</html>

三、第二种写法 onmouseover onmouseout

<!DOCTYPE html>
<html>
<body>
    <div id="box" style="background-color:green;width:120px;height:20px;padding:40px;color:#ffffff;">把鼠标移到上面</div>
    <script src="js/jquery-1.8.3.min.js"></script>
    <script>
        $(function(){
            var sign=document.getElementById("box");
            mouse(sign);
        });
        function mouse(obj){
            obj.onmouseover=function(){ this.className="over"; };//鼠标悬停事件
            obj.onmouseout=function(){ this.className="out"; };//鼠标离开事件
            obj.onmousedown=function(){this.className="down";};//鼠标点击时触发事件
        }
        
    </script>
</body>
</html>

四、鼠标划过,鼠标离开事件

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("p").mouseenter(function(){
    $("p").css("background-color","yellow");
  });
  $("p").mouseleave(function(){
    $("p").css("background-color","#E9E9E4");
  });
  $("#btn1").click(function(){
    $("p").mouseenter();
  });  
  $("#btn2").click(function(){
    $("p").mouseleave();
  }); 
});
</script>
</head>
<body>
<p style="background-color:#E9E9E4">请把鼠标指针移动到段落上。</p>
<button id="btn1">触发段落的 mouseenter 事件</button><br />
<button id="btn2">触发段落的 mouseleave 事件</button>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值