如何使用 jQuery 移除 div 元素内的所有内容?

jqueryweb developmentfront end technology更新于 2026/1/15 22:07:17

要移除 div 元素内的所有内容,请使用 remove() 方法。您可以尝试运行以下代码来移除 <div> 元素内的所有内容 −

示例

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".btn").click(function(){
        $("div#demo").remove();
    });
     
  });
</script>
</head>
<body>

<button class="btn">Hide the elements inside div</button>

<div id="demo">
<p>Inside div- This is demo text.</p>
<p>Inside div- This is demo text.</p>
</div>
<span>
<p>Inside span- This is demo text.</p>
<p>Inside span- This is demo text.</p>
</span>
</body>
</html>

相关文章