2014-02-18 38 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