「jQuery」テキストボックスでfocusinイベントを取得する

2022年2月18日

環境
jquery 3.6.0

書式
$('セレクタ’).focusin(function(){処理コード}
.focusinを使用して、フォーカスが当たったイベントを取得します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function(){
//focusinイベント
$('#username').focusin(function(){
alert('テキストボックスでフォーカスインイベントが発生しました');
});
});
</script>
</head>
<body>
<input type="text" id="username" >
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(function(){ //focusinイベント $('#username').focusin(function(){ alert('テキストボックスでフォーカスインイベントが発生しました'); }); }); </script> </head> <body> <input type="text" id="username" > </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function(){
  //focusinイベント
  $('#username').focusin(function(){
    alert('テキストボックスでフォーカスインイベントが発生しました');
  });
});
</script>
</head>
<body>
  <input type="text" id="username" >
</body>
</html>

結果
テキストボックスにフォーカスが当たった時にalertメッセージを出力します。

jQuery

Posted by arkgame