jquery onメソッドを使用して複数のイベントを登録する

jquery onメソッドを使用して複数のイベントを登録する

jqueryでonメソッドを使って複数のイベントを登録するサンプルコードを記述してます。

環境

  • OS windows10 pro 64bit
  • Apache 2.4.43
  • jquery 3.5.1

※windows10にApacheのインストールはこちら

onメソッド利用

focusイベントとblurイベントの2つを登録してするサンプルコードとなってます。

※CSSは「 bootstrap-material 」を使用してます。

<!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>
.app {
  margin: 0 auto;
  margin-top: 200px;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 30px;
  width: 500px;
}
</style>
<script>
$(document).ready(function(){

  $("#textbox").on({
		"focus":function(e){
			$("#txt").text("フォーカスされました");
		},
		"blur":function(e){
			$("#txt").text("フォーカスが離れました");
		}
	});
  
});

</script>
<body>
  <div class="app">
    <div id="txt" class="alert alert-primary" role="alert">      
    </div>
    <div>
      <form>
        <div class="form-group">
          <input id="textbox" type="text" class="form-control" id="formGroupExampleInput">
        </div>
      </form>
    </div>
    <button id="btn" type="button" class="btn btn-raised btn-warning">count</button>
  </div> 
</body>
</html>

2つイベントが登録されていることが確認できます。