jQuery 複数のイベントを登録するサンプル

環境
Google Chrome 105.0.5195.127
Windows 10 Home 64bit
jquery 3.6.1
構文
$('指定要素’).on('イベント1 イベント2’, function() {処理コード
onメソッドを使って複数のイベントを登録します。
使用例

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.1/jquery.min.js"></script>
<script>
num = 0
$(document).ready(function(){
$('#btnchk').on('click mouseover', function () {
num++;
$('#result').text(`${num}回目のイベント`);
})
});
</script>
</head>
<body>
<h2 id="result"></h2>
<button id="btnchk">検証</button>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> num = 0 $(document).ready(function(){ $('#btnchk').on('click mouseover', function () { num++; $('#result').text(`${num}回目のイベント`); }) }); </script> </head> <body> <h2 id="result"></h2> <button id="btnchk">検証</button> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>

num = 0
$(document).ready(function(){
  $('#btnchk').on('click mouseover', function () {

    num++;
    $('#result').text(`${num}回目のイベント`);

  })
});  
</script>
</head>
<body>
 <h2 id="result"></h2>
 <button id="btnchk">検証</button>

</body>
</html>

実行結果
「検証」ボタンを押すと、マウスオーバーすると、「数字回目のイベント」が表示されます。

jQuery

Posted by arkgame