您能否请一看这段代码,然后告诉我为什么我无法将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>
答案 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中。
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;