Latest web development tutorials

jQuery hover() 方法

jQuery 事件方法 jQuery事件方法

實例

當鼠標指針懸停在上面時,改變<p> 元素的背景顏色:

$("p").hover(function(){
$("p").css("background-color","yellow");
},function(){
$("p").css("background-color","pink");
});

嘗試一下»

定義和用法

hover() 方法規定當鼠標指針懸停在被選元素上時要運行的兩個函數。

jQuery 1.7版本前該方法觸發mouseentermouseleave事件。

jQuery 1.8版本後該方法觸發mouseovermouseout事件。


語法

$(selector).hover(inFunction,outFunction)

調用:

$( selector ).hover( handlerIn, handlerOut )

等同以下方式:

$( selector ).mouseover( handlerIn ).mouseout( handlerOut );

注意:如果只規定了一個函數,則它將會在mouseover和mouseout事件上運行。

調用:

$(selector).hover(handlerInOut)

等同於:

$( selector ).on( "mouseover mouseout", handlerInOut );

參數 描述
inFunction 必需。 規定mouseover 事件發生時運行的函數。
outFunction 可選。 規定mouseout 事件發生時運行的函數。


jQuery 事件方法 jQuery事件方法