简体   繁体   中英

How can I put another text in a cell by using JavaScript?

I would like to put the array price in the same cell of the array antipasti. I've tried too but I substituted the text. I would like to have all of two arrays in the same cell. I didn't know how to do that. Can someone pls tell me how? Thank you so much

Here is the image: https://i.stack.imgur.com/qwOkj.png

function generateTableAntipasti() {
//Build an array containing Customer records.
var antipasti = new Array();
antipasti.push(["Antipasti"]);
antipasti.push(["Patatine"]);
antipasti.push(["Kellogs"]);
antipasti.push(["Ciao"]);
antipasti.push(["Hello"]);
antipasti.push(["Bye"]);

var price = new Array();
price.push(["5,00$"]);
price.push(["5,00$"]);
price.push(["5,00$"]);
price.push(["5,00$"]);
price.push(["5,00$"]);
price.push(["5,00$"]);


//Create a HTML Table element.
var table = document.createElement("Table");
table.border = "1";
table.className = "Antipasti";
table.cellSpacing = 20;

//Add the data rows.
for (var i = 0; i < antipasti.length; i++) {
    row = table.insertRow(-1);
    var cell = row.insertCell(-1);
    cell.innerHTML = antipasti[i];     
}

var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
}

All the above points put together and shortened a little bit...

 function generateTableAntipasti() { //Build an array containing Customer records. var antipasti = ["Antipasti","Patatine","Kellogs","Ciao","Hello","Bye"], price = ["5,00$","5,00$","5,00$","5,00$","5,00$","5,00$"]; //Create a HTML Table element. var table = document.createElement("Table"); table.border = "1"; table.className = "Antipasti"; table.cellSpacing = 8; //Add the data rows. antipasti.forEach((a,i)=>table.insertRow(-1).insertCell(-1).innerHTML = a +'<br>'+price[i]); var dvTable = document.getElementById("dvTable"); dvTable.innerHTML = ""; dvTable.appendChild(table); } generateTableAntipasti()
 <div id="dvTable"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM