jQuery mouseenterとmouseleaveマウスイベントの方法
jQuery mouseenterマウスを乗せた時のイベント方法
mouseenterはマウスを乗せた時に発生するイベントです。
サンプル
$(function(){
$(“.セレクタ”).mouseenter(function(){
処理
});
});
サンプル
<html>
<head>
<script src=”jquery-3.2.1.js”></script>
<script>$(function(){
$(“.botton”).mouseenter(function(){ $(“.three”).css(“color”,”red”)
});
});
</script>
<title>
</title>
</head>
<body>
<input type=”button” value=”botton” class= “botton” >
<div>
<ul><li>1</li>
<li >2</li>
<li class=”three”>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
</body>
</html>
実行結果は
ボタンに乗せた時に3が赤くなります。
一回乗せたらそのまま赤くなったままです。
jQuery mouseleaveマウスイベント外した時のイベントの方法
mouseleaveはマウスを外した時に発生するイベントです。
サンプル
$(function(){
$(“.botton”).mouseleave(function(){ $(“.three”).css(“color”,”black”)
});
});
サンプル
<html>
<head>
<script src=”jquery-3.2.1.js”></script>
<script>$(function(){
$(“.botton”).mouseenter(function(){ $(“.three”).css(“color”,”red”)
});
$(“.botton”).mouseleave(function(){ $(“.three”).css(“color”,”black”)
});
});
</script>
<title>
</title>
</head>
<body>
<input type=”button” value=”botton” class= “botton” >
<div>
<ul><li>1</li>
<li >2</li>
<li class=”three”>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
</body>
</html>
実行結果は
ボタンを乗せると赤になり外すと黒になります。
mouseenterとmouseleaveのイベントの発生要素は指定した要素だけです。