要串联两个字符串,请使用+串联运算符。您可以尝试运行以下代码,以了解如何使用jQuery连接两个字符串-
<!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script> $(function(){ $("a").click(function(){ var id= $(".id").html(); $('.myclass').html("<br><div class='new' id='" + id + "'>Hello</div>"); }); }); </script> </head> <body> <div class="new"> <a href="#">Click me</a> <div class="myclass"></div> <div class="id">Nhooo</div> </div> </body> </html>