【问题标题】:How to convert arrays which contain HTML elements to a table in Javascript?如何将包含 HTML 元素的数组转换为 Javascript 中的表格?
【发布时间】:2017-09-11 17:47:07
【问题描述】:

我有一个数组数组,其中包含 HTML 元素,如 <a> 我想转换为表格,但我的问题是 HTML 显示为原始文本。

首先我认为这是隐藏双引号的问题,所以我将它们替换为.replace()

cell.appendChild(document.createTextNode(x[i][j].replace(/"/g, "")));

但看起来 HTML 在 Chrome DevTools DOM 元素中的格式正确:

<td><a href='http://website.org/prod4'>Product4</a></td>

您能告诉我应该怎么做才能在表格中显示 HTML 元素吗?

JSFiddle

var orderArray = [
    ["1", "29-Aug-2012", "Product1", "client1"],
    ["2", "29-Aug-2012", "Product2", "client2"],
    ["3", "29-Aug-2012", "Product3", "client3"],
    ["4", "29-Aug-2012", "<a href='http://website.org/prod/'>Product4</a>", "client4"],
    ["5", "29-Aug-2012", "Product5", "client5"]
    ];

function display() {
    // get handle on div
    var table = document.createElement("table");
    for (var i = 0; i < orderArray.length; i++) {
        var row = table.insertRow();
        for (var j = 0; j < orderArray[i].length; j++) {
            var cell = row.insertCell();
            cell.appendChild(document.createTextNode(orderArray[i][j]));
        }
    }
    return table;
}
var container = document.getElementById('container');
container.appendChild(display());

我在这个问题中使用了 Bergi 提出的代码:how to display array values inside <table> tag?

谢谢,

【问题讨论】:

  • .createTextNode() 方法正如其名称所暗示的那样:它创建了一个文本节点。这就是浏览器忽略您的 HTML 标记的原因。
  • ... 或设置cell.innerHTML
  • 标题也有点不正确,因为我只看到了一个 String[][] 数组。任何数组中都没有 HTML
  • @StephanBijzitter 你应该再看看。第 4 个数组元素中有一个超链接。
  • @StephanBijzitter 有一些带有标记的表格单元格,例如orderArray[0][3]

标签: javascript html


【解决方案1】:

为了简化整体代码:

container.innerHTML="<table>"+(orderArray.map(row=>"<tr><td>"+row.join("</td><td>")+"</td></tr>").join(""))+"</table>".

【讨论】:

  • @ScottMarcus 加入?
【解决方案2】:

您可以使用 .innerHTML 属性来解决有 HTML 时解析 HTML,没有时注入纯文本的问题。

此外,您可以使用 Array.forEach() 方法作为更简单的循环方式。

var orderArray = [
    ["1", "29-Aug-2012", "Product1", "client1"],
    ["2", "29-Aug-2012", "Product2", "client2"],
    ["3", "29-Aug-2012", "Product3", "client3"],
    ["4", "29-Aug-2012", "<a href='http://website.org/prod4'>Product4</a>", "client4"],
    ["5", "29-Aug-2012", "Product5", "client5"]
];

function display() {
    // get handle on div
    var container = document.getElementById('container');
    
    var table = document.createElement("table");
    
    // Arrays support a forEach method that accepts a callback function
    // to be executed on each item of the array. This function is automatically
    // passed 3 arguments: the current array element, the current index and the array itself.
    // We only need to capture the reference to the array element (value and innerValue in
    // this example).
    orderArray.forEach(function(value) {
        var row = table.insertRow();
        
        value.forEach(function(innerValue) {
            var cell = row.insertCell();
            cell.innerHTML = innerValue;
        });
    });
    
    return table;
}

container.appendChild(display());
td { padding:5px; }
&lt;div id="container"&gt;&lt;/div&gt;

【讨论】:

  • 感谢Array.forEach() 的提示,更优雅。
  • @Florent 不客气。是的,它允许我们在这种情况下完全删除索引。
  • insertRow() 是否有可能指定在 tr 中插入的是 td 还是 th?找不到如何将第一个数组索引声明为表头。编辑哦,我看到有createTHead()
猜你喜欢
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2020-04-29
  • 2021-05-11
  • 2016-09-12
  • 1970-01-01
相关资源
最近更新 更多