无法将动态变量添加到动态表中

时间:2015-11-19 20:14:08

标签: javascript jquery

您能否请一看这段代码,然后告诉我为什么我无法将variable添加到表格中?

var variable = "app ha";
var table = '<table class="table table-striped table-bordered"><tr><td>Energy Target</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Run of River Priorities:</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Species:</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Disturbance:</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Slider:</td><td><span class="'+ variable +'"></span></td></tr></table>';

 $("body").append(table);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

2 个答案:

答案 0 :(得分:1)

您的变量已成功添加为class属性,如果您想在td内显示它,请在列中添加它:

var table = '<table ....
<tr>
    <td>Energy Target</td>
    <td><span class="'+ variable +'">'+ variable +'</span></td>
</tr>
.....</table>';

希望这有帮助。

答案 1 :(得分:0)

不是将变量放在span类中,而是将其添加到span标记的内部HTML中。

&#13;
&#13;
    var variable = "app ha";
    var table = '
 <table class="table table-striped table-bordered">
  <tr>
   <td>Energy Target</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Run of River Priorities:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Species:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Disturbance:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Slider:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
 </table>';

     $("body").append(table);
&#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
&#13;
&#13;
&#13;