简体   繁体   English

如何将包含HTML元素的数组转换为Javascript中的表?

[英]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元素该怎么做吗?

JSFiddle 的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());

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.

相关问题 JavaScript:将包含字符串的数组数组转换为包含对象的数组数组 - JavaScript: convert an array of arrays which contain strings into an array of arrays which contain objects 使用DOM将JavaScript数组转换为HTML表 - Convert javascript arrays into HTML table using DOM 如何对包含null和undefined元素的数组使用assign函数? - How to use assign function with arrays which contain null and undefined elements? 如何使用JavaScript连接HTML元素的两个数组 - How to join two arrays of html elements with JavaScript 如何将HTML表转换为Javascript - How to convert HTML table to Javascript 如何解码包含html元素的javascript变量值 - How to decode javascript variable value which contain html element 如何将单个“n”数字转换为一个数组,该数组将包含 n 个 arrays 并且在每个数组中将包含 n 个带有文本的元素 - How to transform a single "n" number to an array, which will contain n number of arrays and in each Array will be n elements with text 如何使用Javascript表单元素填充html表? - How to populate an html table with Javascript form elements? 如何将文本行拆分为JavaScript数组,其中某些元素用引号引起来并包含逗号? - How to split lines of text into JavaScript Arrays where some elements are enclosed in quotes and contain commas? Javascript 将文本转换为包含 html 标签的超链接 - Javascript convert text to hyperlink that contain html tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM