「jQuery」.clickでクリックイベントを設定する

2022年2月27日

環境
jquery 3.6.0
google chrome 98.0.4758.102
.click()を使用してクリックイベントを記述します。
使用例

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>
$(document).ready(function(){
$("#btnShow").click(function(){
alert('ボタンがクリックされました。');
});
});
</script>
</head>
<body>
<input type="button" value="検証" id="btnShow">
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#btnShow").click(function(){ alert('ボタンがクリックされました。'); }); }); </script> </head> <body> <input type="button" value="検証" id="btnShow"> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#btnShow").click(function(){
    alert('ボタンがクリックされました。');
  });
});
</script>
</head>
<body>
  <input type="button" value="検証" id="btnShow">
</body>
</html>

結果
「検証」ボタンを押すと、アラートが表示されます。

jQuery

Posted by arkgame