【问题标题】:Is it possible to hyperlink JavaScript array elements into an HTML table?是否可以将 JavaScript 数组元素超链接到 HTML 表中?
【发布时间】:2018-01-26 12:36:11
【问题描述】:

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

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>

【问题讨论】:

  • 脚本元素的语言属性在 HTML 4.1 中已被弃用,并在后续版本中被删除。只需将其删除。 type="text/javascript" 不是必需的,它是脚本元素的默认类型。

标签: javascript html arrays html-table


【解决方案1】:

你快到了。你只需要继续添加子元素:

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>

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

【讨论】:

  • 您可以使用row = tbody.insertRow() 和cell = row.insertCell() 稍微减少代码,它们会一次性创建元素并附加它们。你甚至可以做tbody.insertRow().insertCell().appendChild(document.createTextNode('foo'))。 ;-)
  • 谢谢@RobG - 同意。在这种情况下,我不想对 OP 的代码进行太多样式修改。一个对象在这里也更有意义(而不是数据项的数组)。
  • 谢谢@axlj 和@RobG!如果我想在链接之前在我的数组中包含更多列,我应该只创建更多变量并为它们分配 document.createElement("td"); 的值。后跟 variableName.textContext = item[n];?
  • @AndrewC 是的——此外,我用一个示例更新了我的答案,展示了如何改用对象。
  • @axlj 谢谢!由于某种原因,表格格式不适用于带有超链接的单元格,但超链接本身正在工作。我还有一个关于图像的问题......是否可以在表格中附加图像?换句话说,我将如何创建另一个表格单元格并在该特定图像列的每个单元格中放置一个图像?
猜你喜欢
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 2021-04-01
相关资源
最近更新 更多