如何刪除中的所有內容?
若要刪除 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>
廣告