jQuery hover()メソッド
例
マウスポインターが上に置かれたとき、<p> 要素の背景色を変更します:
$("p").hover(function(){
$("p").css("background-color","yellow");
},function(){
$("p").css("background-color","pink");
});
試してみる »
定義と使い方
hover() メソッドは、マウスポインターが選択した要素の上に置かれたときに実行する2つの関数を指定します。
メソッドがトリガーするmouseenterとmouseleaveイベント。
注意:1つの関数だけを指定した場合、mouseenter と mouseleave の両方でそれが実行されます。
構文
$(selector).hover(inFunction,outFunction)
呼び出し:
$( selector ).hover( handlerIn, handlerOut )
以下の方法と同じです:
$( selector ).mouseenter( handlerIn ).mouseleave( handlerOut );
注意:関数を1つだけ指定した場合、それは mouseenter と mouseleave イベントで実行されます。
呼び出し:
$(selector).hover(handlerInOut)
次と同等です:
$( selector ).on( "mouseenter mouseleave", handlerInOut );
| パラメータ | 説明 |
|---|---|
| inFunction | 必須。mouseenter イベント発生時に実行される関数を指定します。 |
| outFunction | 省略可能。mouseleave イベント発生時に実行される関数を指定します。 |
その他の拡張
jQuery イベントメソッド