2017-08-17 35 views
0

这是我迄今为止所做的代码。我想要超链接这些网站,以便当数组以HTML形式输出时,这些网站将是可点击的,并链接到它们各自的网页。出于某种原因,type =“text/javascript”中的代码与language =“JavaScript”中的代码不同,我不知道为什么。如果有人可以提供语言=“JavaScript”的代码,将不胜感激!是否可以将JavaScript数组元素超链接到HTML表格中?

HTML:

<table id="table"> 
    <tr id="tbody"> 
    <th>Mattress Type</th> 
    <th>Link</th> 
    </tr> 
</table> 

JAVASCRIPT:

<script language="JavaScript"> 

var table = document.getElementById("table"); 
var body = document.createElement("tbody"); 

var beds = new Array(3); 
beds[0] = ["Spring Mattress", "King Size", "http://factorymattresstexas.com/specials/spring-air/"]; 
beds[1] = ["Rest Lumbar Support", "Queen Size", "http://factorymattresstexas.com/specials/beautyrest-lumbar-support"]; 
beds[2] = ["Beauty Rest", "Twin Size", "http://factorymattresstexas.com/specials/simmons-beautyrest/"]; 

table.appendChild(tbody); 
beds.forEach(function(items) { 
    var row = document.createElement("tr"); 
    items.forEach(function(item) { 
    var cell = document.createElement("td"); 
    cell.textContent = item; 
    row.appendChild(cell); 
    }); 
    table.appendChild(row); 
}); 
</script> 
+0

脚本元素的语言属性在HTML 4.1中已弃用,并在随后的版本中被删除。只要删除它。 'type =“text/javascript”'不是必需的,它是脚本元素的默认类型。 – RobG

回答

2

你几乎没有。你只需要继续添加子元素:

var table = document.getElementById("table"); 
 
var body = document.createElement("tbody"); 
 

 
// initialize an empty array 
 
var beds = []; 
 

 
// add bed objects to the array 
 
beds.push({ 
 
    type: "Spring Mattress", 
 
    size: "King Size", 
 
    link: "http://factorymattresstexas.com/specials/spring-air/" 
 
}); 
 
beds.push({ 
 
    type: "Rest Lumbar Support", 
 
    size: "Queen Size", 
 
    link: "http://factorymattresstexas.com/specials/beautyrest-lumbar-support" 
 
}); 
 
beds.push({ 
 
    type: "Beauty Rest", 
 
    size: "Twin Size", 
 
    link: "http://factorymattresstexas.com/specials/simmons-beautyrest/" 
 
}); 
 

 
table.appendChild(tbody); 
 
beds.forEach(function(item) { 
 
    var row = document.createElement("tr"); 
 

 
    // You were previously just dumping the whole array contents in a cell. Most likely you want to have separate cells for each type of information. 
 
    var type = document.createElement("td"); 
 
    type.textContent = item.type; 
 

 
    var size = document.createElement("td"); 
 
    size.textContent = item.size; 
 

 
    // Create the containing cell to hold the link 
 
    var link_td = document.createElement("td"); 
 

 
    // Create the <a href="...">...</a> element 
 
    var link = document.createElement("a"); 
 
    link.textContent = item.link; 
 
    link.href = item.link 
 

 
    // Add the link to the cell 
 
    link_td.appendChild(link); 
 

 
    // Add the cells to the row in the order you'd like to see them in 
 
    row.appendChild(type); 
 
    row.appendChild(size); 
 
    row.appendChild(link); 
 

 
    table.appendChild(row); 
 
});
<table id="table"> 
 
    <tr id="tbody"> 
 
    <th>Mattress Type</th> 
 
    <th>Size</th> 
 
    <th>Link</th> 
 
    </tr> 
 
</table>

更新: 你的床阵列字符串数组的数组。我将其切换为使用一系列床对象。这允许您定义属性并通过名称而不是索引来引用这些属性(例如,item.size vs item[1])。随着代码库的增长,这个代码更干净并且可以更好地扩展。您可以使用您要显示的其他属性来扩展床对象。

+0

您可以使用'row = tbody.insertRow()'和'cell = row.insertCell()'来减少代码的数量,并创建元素并一次添加它们。你甚至可以做'tbody.insertRow()。insertCell()。appendChild(document.createTextNode('foo'))'。 ;-) – RobG

+0

感谢@RobG - 同意。在这种情况下,我不想对OP的代码做太多的样式修改。一个对象也会变得更有意义(而不是数据项的数组)。 – axlj

+0

谢谢@axlj和@RobG!如果我想在链接之前在链表中包含更多列,我只需创建更多变量并为它们赋值document.createElement(“td”);其次是variableName.textContext = item [n];? –

相关问题