jquery hover() 鼠标悬停

hover() 方法规定当鼠标指针悬停在被选元素上时要运行的两个函数。

方法触发 mouseenter 和 mouseleave 事件。

注意: 如果只指定一个函数,则 mouseenter 和 mouseleave 都执行它。

语法

$(selector).hover(inFunction,outFunction)

调用:

$( selector ).hover( handlerIn, handlerOut )

等同以下方式:

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

注意:如果只规定了一个函数,则它将会在 mouseenter 和 mouseleave 事件上运行。

调用:

$(selector).hover(handlerInOut)

等同于:

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

参数

  • inFunction : 必需。规定 mouseenter 事件发生时运行的函数。
  • outFunction : 可选。规定 mouseleave 事件发生时运行的函数。

实例:

<html>
<head>
<meta charset="utf-8">
<title>IT懒猫 www.catroom.com.cn</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").hover(function(){
    $("p").css("background-color","yellow");
    },function(){
    $("p").css("background-color","pink");
  });
});
</script>
</head>
<body>

<p>鼠标移动到该段落。</p>

</body>
</html>

效果:
hover效果