jQuery - 要素を削除する

要素/コンテンツを削除する

要素とコンテンツを削除する必要がある場合は、通常、次の 2 つの jQuery メソッドを使用できます:

remove() - 選択した要素 (およびそのサブ要素) を削除します

empty() - 削除選択した要素から子要素を削除します


remove()メソッド

jQueryのremove()メソッドは、選択した要素とその子要素を削除します。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").remove();
  });
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:200px;border:1px solid black;background-color:pink;">
你好
<p>你很好</p>
<p>你太好了</p>
</div>
<br>
<button>移除</button>
</body>
</html>

empty()メソッド

jQuery empty()メソッドは、選択した要素の子要素を削除します。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").empty();
  });
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:200px;border:1px solid black;background-color:yellow;">
你好
<p>你很好</p>
<p>你太好了</p>
</div>
<br>
<button>清空</button>
</body>
</html>

削除された要素をフィルタリングする

jQueryのremove()メソッドはパラメータも受け入れるので、削除された要素をフィルタリングできます。

このパラメーターには、任意の jQuery セレクター構文を使用できます。

りー


学び続ける
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("input:button").click(function() { $("input:checkbox:checked").next().nextAll().remove(); }); }) </script> <style type="text/css"> div.box{width:300px;height:200px;padding:10px 20px;border:4px dashed #ccc;} div.box>span{color:#999;font-style:italic;} div.content{width:250px;height:100px;margin:10px 0;border:1px solid green;} input{margin:10px;} input[type='button']{width:200px;height:35px;margin:10px;border:2px solid #ebbcbe;} </style> </head> <body> <div class="box"> <span>点击按钮后,删除被选项目之后的所有选项。</span><br> <div class="content"> <input type="checkbox" name="item"><span>萝卜</span> <input type="checkbox" name="item"><span>青菜</span> <input type="checkbox" name="item"><span>小葱</span><br> <input type="checkbox" name="item"><span>豆腐</span> <input type="checkbox" name="item"><span>土豆</span> <input type="checkbox" name="item"><span>茄子</span><br> </div> <input type="button" value="删除"> </div> </body> </html>
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!