jQuery hover() 方法
實例
當滑鼠指針懸停在上面時,改變 <p> 元素的背景顏色:
$("p").hover(function(){
$("p").css("background-color","yellow");
},function(){
$("p").css("background-color","pink");
});
定義和用法
hover() 方法規定當滑鼠指針懸停在被選元素上時要運行的兩個函數。
方法觸發 mouseenter 和 mouseleave 事件。
注意: 如果只指定一個函數,則 mouseenter 和 mouseleave 都執行它。
語法
$(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 事件發生時運行的函數。 |