[英]How to convert arrays which contain HTML elements to a table in Javascript?
I have an array of arrays which contain HTML elements like <a>
I would like to convert to a table but my problem is that HTML is displayed as raw text. 我有一个数组数组,其中包含HTML元素,例如
<a>
我想转换为表,但是我的问题是HTML显示为原始文本。
First I supposed it was a problem with hidden double quotes so I replace them with .replace()
首先,我认为这是隐藏双引号的问题,因此我将其替换为
.replace()
cell.appendChild(document.createTextNode(x[i][j].replace(/"/g, "")));
But it looks like HTML is corretly formated in the Chrome DevTools DOM element : 但看起来HTML是在Chrome DevTools DOM元素中正确格式化的:
<td><a href='http://website.org/prod4'>Product4</a></td>
Could you please tell me what should I do to display HTML elements in my table ? 您能告诉我在表中显示HTML元素该怎么做吗?
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());
I use the code proposed by Bergi in this question : how to display array values inside <table> tag? 我在这个问题中使用了Bergi提出的代码: 如何在<table>标签内显示数组值?
Thank you, 谢谢,
You can use the .innerHTML
property to solve the issue of parsing the HTML, when there is HTML and injecting the plain text when there isn't. 您可以使用
.innerHTML
属性来解决在存在HTML时解析HTML以及在没有HTML时注入纯文本的问题。
Additionally, you can use the Array.forEach()
method as an easier way to loop. 此外,您可以使用
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; }
<div id="container"></div>
为了简化整体代码:
container.innerHTML="<table>"+(orderArray.map(row=>"<tr><td>"+row.join("</td><td>")+"</td></tr>").join(""))+"</table>".
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.