jquery hoverメソッドを使ってマウスのホバーと離れたイベントを取得する
jqueryでhoverメソッドを使ってマウスが要素にカーソルが当たったときと離れたときのイベントを取得するとサンプルコードとなります。
環境
- OS windows10 pro 64bit
- Apache 2.4.43
※windows10にApacheのインストールはこちら
hover利用
要素に、マウスをホバーした時と離れた時のイベント発生時にテキストを変更するサンプルコードとなります。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>mebeeサンプル</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">
<link rel="stylesheet" href="https://unpkg.com/bootstrap-material-design@4.1.1/dist/css/bootstrap-material-design.min.css" integrity="sha384-wXznGJNEXNG1NFsbm0ugrLFMQPWswR3lds2VeinahP8N0zJw9VWSopbjv2x7WCvX" crossorigin="anonymous">
<script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<style>
.container {
margin: 0 auto;
margin-top: 300px;
display: flex;
flex-direction: column;
align-items: center;
font-size: 30px;
}
</style>
<script>
$(function(){
$('.alert-primary').hover(
//マウスがホバーときのイベント
function(){
$('.alert-primary').text("マウスがホバーされました");
},
//マウスが離れたときのイベント
function(){
$('.alert-primary').text("マウスが外れました");
}
);
});
</script>
<body>
<div class="container">
<div id="piyo"></div>
<div class="alert alert-primary" role="alert">
ホバーしてください
</div>
</div>
</body>
</html>
実行結果を確認するとマウスをホバーした時と離れた時でテキストが変更されることが確認できます。
-
前の記事
受信の規則・送信の規則の設定画面へのショートカット 2020.11.09
-
次の記事
javascript アロー関数を使用して重複した配列を除去する 2020.11.09
コメントを書く