jQuery appendで指定の要素の子要素の最後に追加する

環境
jQuery 3.6.4
Windows 10 Home 64bit

構文
$(追加先).append(追加する要素)

使用例
$(“#cft").append('<p id = “cft">東京</p>’);とした場合、
<div id="cft">の子要素の最後に<p id = “cft">東京</p>を追加します。

サンプルコード

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.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#chk").click(function(){
$("#cft").append('<p id="cft">東京</a>');
});
});
</script>
</head>
<body>
<div id="cft">
<p id="p4">tokyo</p>
</div>
<button id="chk">追加</button>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script> $(document).ready(function(){ $("#chk").click(function(){ $("#cft").append('<p id="cft">東京</a>'); }); }); </script> </head> <body> <div id="cft"> <p id="p4">tokyo</p> </div> <button id="chk">追加</button> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#chk").click(function(){
     $("#cft").append('<p id="cft">東京</a>');
  });
});
</script>
</head>
<body>

<div id="cft">
  <p id="p4">tokyo</p>
</div>
<button id="chk">追加</button>
</body>
</html>

実行結果
「追加」ボタンを押すと、<p id = “cft">東京</p>を追加します。

jQuery

Posted by arkgame