jQueryでのイベント取得と画像切替
今回はマウスが置かれたときと離されたときに画像が切り替わるイベントを
jQueryを使うとjavascriptだけで書くときと微妙に違っていたので書いておく。
普通にjavascriptで書くと
<html> <input type="image" src="image" id="button" onmouseover="hoge();" onmouseout="huga();"> </html> <script> var btn = document.getElementById("button") function hoge(){ btn.src = "image2"; } function huga(){ btn.src = "image"; } </style>
文法うんぬんの問題は置いておいて...
やり方は人それぞれだからあれだけど多分こんなとこなのかな??
jQuery使うと
<html> <input type="image" src="image" id="button"> </html> <script> $(document).ready(function(){ $("#image").mouseover(funtion(){ $(this).attr("src","image2"); }); $("#image").mouseout(function(){ $(this).attr("src","image"); }); </script>
こんな感じかな。
これが最善の方法なのか分からないけど。
もっと、スマートな方法とかあったりするのかな??