jQuery mouseleaveメソッドを使ってマウスが離れたイベントを取得する
環境
Google Chrome 105.0.5195.127
Windows 10 Home 64bit
jquery 3.6.1
構文
$('指定要素’).on('mouseleave’, function() {処理コード
onメソッドに「mouseleave」を指定してマウスが離れたイベントを取得します。
使用例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#tmd').on('mouseleave', function() {
$('#result').text( '指定要素からマウスが離れました' );
})
});
</script>
</head>
<body>
<h2 id="result"></h2>
<a id="tmd" >test data</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#tmd').on('mouseleave', function() {
$('#result').text( '指定要素からマウスが離れました' );
})
});
</script>
</head>
<body>
<h2 id="result"></h2>
<a id="tmd" >test data</a>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#tmd').on('mouseleave', function() { $('#result').text( '指定要素からマウスが離れました' ); }) }); </script> </head> <body> <h2 id="result"></h2> <a id="tmd" >test data</a> </body> </html>
実行結果
要素「tmd」の文字「test data」からマウスが離れたら、
「指定要素からマウスが離れました」というテキストが表示されます。