如何在jQuery中使用字符串中的变量 [英] how to use variable in string in jquery

查看:1299
本文介绍了如何在jQuery中使用字符串中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个字符串中使用一个变量。

 < head>我已经尝试了很多次,但它只是获取变量名称。 
< script type =text / javascriptsrc =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 =#>制作Html< / a>
< div class =html>< / div>
< div class =id>第一个< / div>
< / div>
< / body>


解决方案

连接可以用 + 如下。

  $('。html')。html(< div class ='new'id ='+ id +'> jitender< / div>); 

参考:


I want to use a variable in a string. I have tried to do it many times, but it is only getting the variable name.

<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>

解决方案

Concatenating can be done with + as follows.

$('.html').html("<div class='new' id='" + id + "'>jitender</div>");

Reference:

这篇关于如何在jQuery中使用字符串中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆