要动态创建div,请使用html()
方法。您可以尝试运行以下代码,以了解如何在单击按钮时动态创建div-
<!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("body").html("<div>This is a new div.</div>"); }); }); </script> </head> <body> <button>Create a new div</button> <p>This is demo text.</p> </body> </html>