empty()方法从匹配元素集中删除所有子节点,而remove(expr)方法从DOM中删除所有匹配元素。
您可以尝试运行以下代码,以了解如何在jQuery中删除DOM元素:
<html> <head> <title>jQuery Example</title> <script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function() { $("div").click(function () { $(this).remove( ); }); }); </script> <style> .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;} </style> </head> <body> <p>Click on any square below:</p> <span id = "result"> </span> <div class = "div" style = "background-color:blue;"></div> <div class = "div" style = "background-color:green;"></div> <div class = "div" style = "background-color:red;"></div> </body> </html>