在jquery中提供了.hover()事件,以简化dom中的mouseenter(鼠标进入),和mouseleave(鼠标离开)事件,hover的第一个参数(匿名方法)表示mouseenter,第二个参数表示mouseleave,即表示可以为hover传递两个参数。如下代码所示:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hover demo</title>
<style>
ul {
margin-left: 20px;
color: blue;
}
li {
cursor: default;
}
span {
color: red;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.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(//为li绑定了鼠标进入和鼠标移开的两个参数
function() {
$( this ).append( $( "<span> ***</span>" ) );
}, function() {
$( this ).find( "span:last" ).remove();
}
);
$( "li.fade" ).hover(function() {<pre name="code" class="html" style="color: rgb(51, 51, 51);
font-size: 14px; line-height: 26px;">//为li元素下的class类是fade的所有元素绑定了鼠标进入参数
$( this ).fadeout( 100 );
$( this ).fadein( 500 );});
</script>
</body>
</html>
以上就是关于jquery鼠标悬停事件.hover()的使用详解的详细内容。