如何使用 jQuery 從父元素中移除所有子節點?
若要從父元素中刪除所有子節點,請使用 empty() 方法。empty() 方法從匹配元素的集合中刪除所有子節點。
示例
你可以嘗試執行以下程式碼,瞭解如何從父元素中刪除所有子節點 −
<html> <head> <title>jQuery empty() method</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function() { $("div").click(function () { $(this).empty(); }); }); </script> <style> .div { margin:10px; padding:12px; border:2px solid #666; width:60px; } </style> </head> <body> <p>Click on any square below to see the result:</p> <div class = "div" style = "background-color:yellow;">ONE</div> <div class = "div" style = "background-color:gray;">TWO</div> </body> </html>
廣告