jQuery removeメソッドで要素を削除するサンプル

環境
jQuery 3.6.0
Windows 10 Home 64bit

構文
$(セレクタ).remove();
removeメソッドは、指定した要素とその子要素を削除します。
使用例

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(){
$("#btndel").click(function () {
$(".city").remove();
});
});
</script>
</head>
<body>
<div id="sample1">東京
<div class="city">大阪
<p>福岡</p>
</div>
</div>
<input type="button" id="btndel" value="削除" />
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(function(){ $("#btndel").click(function () { $(".city").remove(); }); }); </script> </head> <body> <div id="sample1">東京 <div class="city">大阪 <p>福岡</p> </div> </div> <input type="button" id="btndel" value="削除" /> </body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function(){
   $("#btndel").click(function () {
    $(".city").remove();
  });
});
</script>
</head>
<body>


<div id="sample1">東京
  <div class="city">大阪
    <p>福岡</p>
  </div>
</div>
<input type="button" id="btndel" value="削除" />

</body>
</html>

実行結果
「削除」ボタンを押すと、div要素class「city」の文字「大阪」、「福岡」が削除されます。

jQuery

Posted by arkgame