javascript - 在表格中显示 Javascript 链接

标签 javascript html css

<分区>


这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minimal example在问题本身。

关闭 9 年前

您好,我正在尝试在表格中显示我的链接。这些链接是从 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/

上一篇:css - 使 CSS 文件中的所有规则仅适用于 id 的后代

下一篇:html - :before work in css?如何

相关文章:

javascript - 如何加入 Brightcove 托管视频结束时触发的事件?

javascript - Firefox 是否允许使用 "aggregated"火焰图分析 JS 应用程序?

javascript - 在div中动态创建表格

javascript - KnockOut.JS 使用 observable 的值填充下拉列表

javascript - 拖动元素和 mouseout 事件

html - 如何防止表格单元格中的文本换行

css - .visible-xs 不在 div 内居中

javascript - 在 JavaScript switch 语句中使用 OR 运算符

javascript - jQuery Mobile 1.4.2 - 带图标的单选按钮

javascript - HTML5 Canvas - 从左上角绘制圆圈