<分区>
标签 javascript html css
<分区>
您好,我正在尝试在表格中显示我的链接。这些链接是从 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>
随着链接列表的增长,我还需要增加行数!
这是一个 fiddle :http://jsfiddle.net/volterony/5nW86/
伏尔托尼
最佳答案
我认为您需要使事情比当前 fiddle 中的事情简单得多。有很多事情要做,当你只是制作 HTML 时,你不需要做那么多。我修改了你的jsfiddle你可以看到我动态构建了 HTML。
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
现在基础知识已经完成,您应该能够将任何缺失的部分添加到我提供的基本模板中(例如添加 anchor 链接等)。当您可以自己编写 HTML 时,有时使用 document
API 可能有点令人生畏和矫枉过正。
关于javascript - 在表格中显示 Javascript 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852178/