2014-02-18 40 views
0

嗨,我正在尝试在表格中显示我的链接。链接从JSON数据源中获取,我想只在td标签中显示品牌名称。目标将是:在表格中显示Javascript链接

<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/

Volterony

+0

你有没有考虑过使用模板库? – Blowsie

+0

另外'colspan ='不是表的有效属性。 http://www.w3schools.com/tags/tag_table.asp – Blowsie

+0

不幸的是,我只限于纯粹的JS – Volterony

回答

2

我认为你需要做的事情比你必须在当前的拨弄什么简单得多。有很多事情要做,而且你只需制作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 

现在基本完成后,你应该能够在任何丢失部分我公司提供的基本模板添加(如添加锚链接等)。有时候使用document API可能会让你感到有点畏缩和过度杀伤,因为你可以自己编写HTML。

+0

嗨德伊夫感谢您提供的例子。我会尝试将链接合并到您的基本模板中。我只是一个Javascript新手,所以我会看到我如何得到 – Volterony

+0

Deif我试图添加链接,但他们似乎不加载品牌内容? http://jsfiddle.net/volterony/5nW86/ – Volterony

+0

你的小提琴没有包含我建议的任何东西。请尝试写一些HTML。 – Deif