.hover()
.hover( handlerIn(eventObject), handlerOut(eventObject) ) 返回: jQuery
描述: 將二個(gè)事件函數(shù)綁定到匹配元素上,分別當(dāng)鼠標(biāo)指針進(jìn)入和離開元素時(shí)被執(zhí)行。
-
version added: 1.0.hover( handlerIn(eventObject), handlerOut(eventObject) )
handlerIn(eventObject)當(dāng)鼠標(biāo)指針進(jìn)入元素時(shí)觸發(fā)執(zhí)行的事件函數(shù)
handlerOut(eventObject)當(dāng)鼠標(biāo)指針離開元素時(shí)觸發(fā)執(zhí)行的事件函數(shù)
.hover()方法是同時(shí)綁定 mouseenter和 .hover()事件。我們可以用它來簡單地應(yīng)用在鼠標(biāo)在元素上活動(dòng)行為。
調(diào)用$(selector).hover(handlerIn, handlerOut)是以下寫法的簡寫:
$(selector).mouseenter(handlerIn).mouseleave(handlerOut);
更多細(xì)節(jié)參見.mouseenter() 和 .mouseleave()。
Examples:
Example: 當(dāng)鼠標(biāo)在列表中來回滑動(dòng)的時(shí)候添加特殊的樣式, try:
<!DOCTYPE html>
<html>
<head>
<style>
ul { margin-left:20px; color:blue; }
li { cursor:default; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.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>
Demo:
Example: 當(dāng)鼠標(biāo)在表格單元格中來回滑動(dòng)的時(shí)候添加特殊的樣式, try:
$("td").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
Example: 解除綁定上面的例子中使用:
$("td").unbind('mouseenter mouseleave');
.hover( handlerInOut(eventObject) ) 返回: jQuery
描述: 將一個(gè)單獨(dú)事件函數(shù)綁定到匹配元素上,分別當(dāng)鼠標(biāo)指針進(jìn)入和離開元素時(shí)被執(zhí)行。
-
version added: 1.4.hover( handlerInOut(eventObject) )
handlerInOut(eventObject)當(dāng)鼠標(biāo)指針進(jìn)入或離開元素時(shí)觸發(fā)執(zhí)行的事件函數(shù)
當(dāng)傳遞個(gè).hover() 方法一個(gè)單獨(dú)的函數(shù)的時(shí)候,將執(zhí)行同時(shí)綁定 mouseenter和 .hover()事件函數(shù)。這允許在處理函數(shù)中用戶使用jQuery的各種切換方法。
調(diào)用$(selector).hover(handlerInOut)是以下寫法的簡寫:
$(selector).bind("mouseenter mouseleave",handlerInOut);
更多細(xì)節(jié)參見.mouseenter() 和 .mouseleave()。
Example:
Slide the next sibling LI up or down on hover, and toggle a class.
<!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="http://code.jquery.com/jquery-latest.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>