返回值:jQueryhover(handlerIn(eventObject), handlerOut(eventObject))

为匹配的元素绑定两个事件,分别在鼠标指针进入和离开元素时被触发。

.hover() 事件绑定两个事件,分别是 mouseentermouseleave 事件。使用该事件可以很简单的为元素绑定鼠标移入移出时的行为。

$(selector).hover(handlerIn, handlerOut) 是下列用法的便捷方式:

$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

更多详细内容,请参阅 .mouseenter() .mouseleave()

示例:

鼠标悬停在列表项上时,为其添加一个特殊的样式:

<!DOCTYPE html>
<html>
<head>
<style>
  ul { margin-left:20px; color:blue; }
  li { cursor:default; }
  span { color:red; }
</style>
<script src="jquery.min.js"></script>
</head>
<body>

<ul>
    <li>Milk</li>
    <li>Bread</li>
    <li class='fade'>Chips</li>

    <li class='fade'>Socks</li>
  </ul>

<script>


$("li").hover(
  function () {
    $(this).append($("<span> ***</span>"));
  }, 
  function () {
    $(this).find("span:last").remove();
  }
);



//li with fade class
$("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});



</script>
</body>
</html>

演示:

示例:

鼠标悬停在 td 上时,为其添加一个特殊的样式:

jQuery 代码:
$("td").hover(
  function () {
    $(this).addClass("hover");
  },
  function () {
    $(this).removeClass("hover");
  }
);

示例:

解除上例中绑定的事件:

jQuery 代码:
$("td").unbind('mouseenter mouseleave');

返回值:jQueryhover(handlerInOut(eventObject))

为匹配的元素绑定一个函数,在鼠标指针进入和离开元素时会触发该函数。

当向 .hover() 方法只传入一个函数时,相当于为 mouseentermouseleave 绑定了相同的事件。可以在该函数内使用 jQuery 各种各样的切换方法,或根据 event.type,作出不同的响应。

$(selector).hover(handlerInOut) 是下列用法的便捷方式:

$(selector).bind("mouseenter mouseleave", handlerInOut);

更多详细内容,请参阅 .mouseenter() .mouseleave()

示例:

向上或向下滑动显示或隐藏下一个兄弟 LI 节点,并切换样式。

<!DOCTYPE html>
<html>
<head>
<style>
  ul { margin-left:20px; color:blue; }
  li { cursor:default; }
  li.active { background:black;color:white; }
  span { color:red; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>

<ul>
    <li>Milk</li>
    <li>White</li>
    <li>Carrots</li>
    <li>Orange</li>
    <li>Broccoli</li>
    <li>Green</li>
  </ul>

<script>


$("li")
.filter(":odd")
.hide()
 .end()
.filter(":even")
.hover(
  function () {
    $(this).toggleClass("active")
      .next().stop(true, true).slideToggle();
  }
);




</script>
</body>
</html>

演示: