jQueryでマウスオーバーで画像を切り替えるサンプルです。
サンプルソース
例)画像にカーソルを当てると画像が切り替わる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>サンプル</title> <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script> <script> $(function(){ $('#img1').mouseover(function(){ $(this).attr('src', 'im1.png'); }); $('#img1').mouseleave(function(){ $(this).attr('src', 'im2.png'); }); }); </script> </head> <body> <img src="im2.png" id="img1"> </body> </html> |
実行サンプル
画像にカーソルを当てると画像が切り替わります。

解説
- 画像に対して mouseover/mouseleave イベントを指定して画像を切り替えています。