嗨,我正试着在表格中显示我的链接。链接是从JSON数据源中提取的,我只想在td标记中显示品牌名称。我们的目标是:
<table border="1" colspan="5">
<tr><td>Nike</td><td>Puma</td><td>etc</td><td>etc</td><td></td><td></td></tr>
</table>我也需要随着链接列表的增长而增加行数!
这里有一个小提琴:http://jsfiddle.net/volterony/5nW86/
沃特罗尼
发布于 2014-02-18 20:35:38
我认为你需要让事情变得比你现在的小提琴简单得多。有很多事情要做,当你只是做HTML的时候,你不需要做这么多事情。我修改了你的超文本标记语言,你可以看到我是动态构建jsfiddle的。
var company, link, html = "<tr>", cols = 4, currentCol = 1;
for (company in brand) // Loop through each item in the array
{
if (currentCol > cols) { // Make sure we only have 4 columns
currentCol = 1; // Reset the current column if we go over that
html += "</tr><tr>"; // Stop the previous row and start a new one
}
html += "<td>" + company + "</td>"; // Add the current item to the table
currentCol++; // Increment the column count for the next loop
}
html += "</tr>";
document.getElementById('table').innerHTML = html; // Append the html with our dynamically created html现在基础工作已经完成,你应该能够添加任何缺少的部分,我提供的基本模板(如添加在锚链接,等等)。有时,当您可以自己编写HTML时,使用HTML可能有点令人望而生畏。
https://stackoverflow.com/questions/21852178
复制相似问题