jQuery hover() 方法
实例
当鼠标指针悬停在 <p> 元素上时,更改该元素的背景色:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("p").hover(function(){$(this).css("background-color", "yellow");}, function(){$(this).css("background-color", "pink");});});</script></head><body><p>将鼠标指针悬停在该段落上。</p></body></html>
定义与用法
hover() 方法指定鼠标指针悬停在选定元素上时要运行的两个函数。
该方法会触发 mouseenter 和 mouseleave 事件。
注意:如果只指定了一个函数,它将对 mouseenter 和 mouseleave 事件运行。
语法
$(selector).hover(inFunction,outFunction)
| 参数 | 描述 |
|---|---|
| inFunction | 必填。指定发生 mouseenter 事件时要运行的函数 |
| outFunction | 可选。指定 mouseleave 事件发生时要运行的函数 |