返回值:jQueryhover(handlerIn(eventObject), handlerOut(eventObject))
Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.
-
1.0 新增hover(handlerIn(eventObject), handlerOut(eventObject))
handlerIn(eventObject) (Function) A function to execute when the mouse pointer enters the element.handlerOut(eventObject) (Function) A function to execute when the mouse pointer leaves the element.
The .hover()
method binds handlers for both mouseenter
and mouseleave
events. We can use it to simply apply behavior to an element during the time the mouse is within the element.
Calling $(selector).hover(handlerIn, handlerOut)
is shorthand for:
$(selector).mouseenter(handlerIn).mouseleave(handlerOut);
See the discussions for .mouseenter()
and .mouseleave()
for more details.
示例:
To add a special style to list items that are being hovered over, try:
<!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>
演示:
示例:
To add a special style to table cells that are being hovered over, try:
jQuery 代码:
$("td").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
示例:
To unbind the above example use:
jQuery 代码:
$("td").unbind('mouseenter mouseleave');