解决click与hover(mouseover)的冲突问题

本文介绍了一个使用jQuery实现的元素鼠标悬停效果案例。通过jQuery的.data()方法设置和读取元素的数据属性来控制不同的样式变化,并结合.mouseover()和.mouseout()事件触发不同的UI反馈。

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

主要应用到一个知识点:$(selector).data('name','value');

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style type="text/css">
      .content{
          width:100px;
          height:100px;
          border:2px solid #999;
          border-radius:10px;
       }
       .active{
        background:red;
       }
       .onbackground{
        background:green;
       }
</style>


<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body >
<div class="content">
     <button class="seemore">查看</button>
  </div>
    <script>
       
      $('.content').mouseover(function(){
           
           var a = $(".content").data("flag");
             console.log(a);
             if(a==1){
              
             }else{
               $(this).addClass("onbackground");
             }
           
        });


        $(".content").mouseout(function(){


              $(this).removeClass("onbackground");
          })
        
        $(".seemore").click(function(){
           $(this).parent().addClass("active");
           $(".content").data("flag","1");
        })


        
          
        
    </script>
    


</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值