1

Herro,我正在尝试通过 Javascript 生成一个 html 表,但是我的输出失败了:

HTML:

<div id="racks" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="overflow: auto; display: block; height: 196.8px;" aria-labelledby="ui-accordion-accordion-header-2" role="tabpanel" aria-expanded="true" aria-hidden="false">
<table border="1">
<tbody>
<tr></tr>
</tbody>
</table>
<a onclick="alert(LSZ09 )" href="#">*</a>
<a onclick="alert(LSZ10 )" href="#">*</a>

</div>

Javascript:

document.getElementById("racks").innerHTML = "<table border='1'><tbody><tr>";
                        for (var i=0;i<16;i++)
                            {
                            document.getElementById("racks").innerHTML = document.getElementById("racks").innerHTML +"<td><a href='#' onClick='alert('"+jPunten[i].STATDEV+"')'>*</a></td>";
                            }
                            document.getElementById("racks").innerHTML = document.getElementById("racks").innerHTML + "</tr></table>";
4

1 回答 1

0

您可以在最后更新#racks 元素。无需每次迭代都更新它。而且您在onClick活动的报价中也遇到了问题

尝试这样的事情:

html = "<table border='1'><tbody><tr>";
for (var i=0;i<16;i++) {
  html += "<td><a href='#' onClick='alert(\""+jPunten[i].STATDEV+"\")'>*</a></td>";
}
html += "</tr></table>";
document.getElementById("racks").innerHTML = html;
于 2013-10-29T13:20:24.103 回答