我想在字符串中使用变量.我曾尝试多次这样做,但它只获取变量名称.
<head> <script type="text/javascript" src="jquery-1.7.2.js"></script> <script type="text/javascript"> $(function(){ $("a").click(function(){ var id= $(".id").html(); $('.html').html("<div class='new' id=+id+>jitender</div>") }); }); </script> </head> <body> <div class="wrap"> <a href="#">Make Html</a> <div class="html"></div> <div class="id">first</div> </div> </body>
解决方法
连接可以如下进行.
$('.html').html("<div class='new' id='" + id + "'>jitender</div>");
参考:
> https://developer.mozilla.org/en/JavaScript/Reference/Operators/String_Operators