如何刪除
中的所有內容?


若要刪除 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>

更新於:2020 年 2 月 14 日

562 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始
廣告