Jquery悬停功能显示和隐藏元素

时间:2016-04-25 07:19:17

标签: javascript jquery html css

我有一个悬停功能,可以显示和隐藏我的子菜单,直到有人在它们上面悬停。

问题是,当我尝试将鼠标移动到显示它再次消失的菜单时。

有谁可以指出我在这里做错了什么。?



$(document).ready(function() {
  $('.menu-Link > a').hover(function() {
    $(this).next('.menu').show();
  }, function() {
    $(this).next('.menu').hide();
  });
});

nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  /*background-color: #333333;*/
  background-color: transparent;
  transition: 0.2s;
}
nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: transparent;
  display: inline-block;
}
nav ul li {
  float: left;
}
nav ul li a.link {
  display: block;
  color: black;
  text-align: center;
  padding: 24px 16px;
  text-decoration: none;
  transition: 0.2s;
  font-weight: bold;
  text-transform: uppercase;
  position: relative;
}
nav ul li div.arrow-down {
  float: right;
  width: 0;
  height: 0;
  margin-left: 5px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #fff;
  margin-top: 10px;
  transition: 0.2s;
}
nav ul li div.menu {
  display: none;
  z-index: 999;
  position: relative;
  background-color: #fff;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
  padding: 15px 0;
  margin-top: -15px;
}
nav ul li div.menu a {
  color: #333333;
  padding-left: 15px;
}
nav ul li a.link:hover {
  color: #808080;
  text-decoration: none;
  transition: 0.2s;
}
nav ul li a.link:hover div.arrow-down {
  border-top: 5px solid #808080;
  transition: 0.2s;
}
nav ul li:first-child {
  padding-left: 100px;
}
nav ul.right {
  float: right;
}
nav ul.right li:last-child {
  padding-right: 100px;
}
nav.color {
  background-color: #333333;
  transition: 0.2s;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <ul class="left">
    <li class="menu-Link">
      <a href="" class="link">Home</a>
      <div class="menu">
        <a href="">subcat.Title</a>
      </div>
    </li>
    <li class="menu-Link">
      <a href="" class="link">Test</a>
      <div class="menu">
        <a href="">subcat.Title</a>
      </div>
    </li>
  </ul>
  <ul class="right">
    <li><a href="/">Basket</a>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:5)

问题是因为该事件已附加到UPDATE Kho /*SET Kho.Xuat = SUM(PhieuXuat.SL)*/ SET Kho.Xuat = sumSL FROM Kho INNER JOIN (SELECT SUM(PhieuXuat.SL) as sumSL,PhieuXuat.MaVT FROM PhieuXuat group by PhieuXuat.MaVT )P ON (Kho.MaVT = P.MaVT) 元素,因此当用户尝试将鼠标悬停在显示的菜单上时,a上会调用mouseout部分悬停并且JS代码试图再次隐藏菜单。要解决此问题,请将a事件附加到同时包含hover()和菜单的元素。在这种情况下,a元素本身。您还需要将.menu-link更改为next()以满足更改需求。试试这个:

&#13;
&#13;
find()
&#13;
$(document).ready(function() {
  $('.menu-Link').hover(function() {
    $(this).find('.menu').show();
  }, function() {
    $(this).find('.menu').hide();
  });
});
&#13;
nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  /*background-color: #333333;*/
  background-color: transparent;
  transition: 0.2s;
}
nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: transparent;
  display: inline-block;
}
nav ul li {
  float: left;
}
nav ul li a.link {
  display: block;
  color: black;
  text-align: center;
  padding: 24px 16px;
  text-decoration: none;
  transition: 0.2s;
  font-weight: bold;
  text-transform: uppercase;
  position: relative;
}
nav ul li div.arrow-down {
  float: right;
  width: 0;
  height: 0;
  margin-left: 5px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #fff;
  margin-top: 10px;
  transition: 0.2s;
}
nav ul li div.menu {
  display: none;
  z-index: 999;
  position: relative;
  background-color: #fff;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
  padding: 15px 0;
  margin-top: -15px;
}
nav ul li div.menu a {
  color: #333333;
  padding-left: 15px;
}
nav ul li a.link:hover {
  color: #808080;
  text-decoration: none;
  transition: 0.2s;
}
nav ul li a.link:hover div.arrow-down {
  border-top: 5px solid #808080;
  transition: 0.2s;
}
nav ul li:first-child {
  padding-left: 100px;
}
nav ul.right {
  float: right;
}
nav ul.right li:last-child {
  padding-right: 100px;
}
nav.color {
  background-color: #333333;
  transition: 0.2s;
}
&#13;
&#13;
&#13;

另请注意,您可以单独使用CSS实现此功能而无需任何JS代码,只需删除<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul class="left"> <li class="menu-Link"> <a href="" class="link">Home</a> <div class="menu"> <a href="">subcat.Title</a> </div> </li> <li class="menu-Link"> <a href="" class="link">Test</a> <div class="menu"> <a href="">subcat.Title</a> </div> </li> </ul> <ul class="right"> <li><a href="/">Basket</a> </li> </ul> </nav>处理程序并将此行添加到CSS中:

hover()