【问题标题】:Trigger an un-hover?触发取消悬停?
【发布时间】:2021-10-30 07:52:05
【问题描述】:

在下面的脚本中,第一个 div 显示,第二个隐藏。但是在第一个 div 悬停时,它被隐藏,第二个 div 显示。将第二个 div 悬停不会再次显示第一个 div - 我相信因为它是隐藏的,因此悬停功能所涵盖的“mouseout”功能不适用于隐藏的 div。我尝试添加单独的 mouseLeave 和 mouseout 事件,但都无法正常工作。有什么想法吗?

$(document).ready(function(){
  $("#seconddiv").hide();
  $("#firstdiv").hover(function(){
    $("#seconddiv").show();
    $("#firstdiv").hide();
  });
});
.fullwidth {
width: 100%;
padding-top: 25px;
padding-bottom: 25px:
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="firstdiv" class="fullwidth">
<p>First Text</p>
</div>
<div id="seconddiv" class="fullwidth">
<p>Second Text</p>
</div>
</body>
</html>

【问题讨论】:

    标签: jquery hover mouseleave mouseout


    【解决方案1】:

    Hover 有 2 个函数。第一个输入处理第二个处理输出

    在您的代码示例中使用它:

      $("#firstdiv").hover(function(){
        $("#seconddiv").show();
        $("#firstdiv").hide();
      },
      function(){
        $("#seconddiv").hide();
        $("#firstdiv").show();  
      }) 
    

    这是一个例子https://jsfiddle.net/L8h1gawy/

    在此处查看更多详细信息:https://api.jquery.com/hover/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 2020-08-27
      • 2013-03-28
      • 1970-01-01
      相关资源
      最近更新 更多