How to Generate 3x3 HTML Table From an JavaScript Array
I have an array of values in Javascript as f开发者_Go百科ollows:
var data = {"A", "B", "C", "D", "E", "F", "G", "H", "I" };
How can I generate a 3x3 HTML table like the following
A B C
D E F
G H I
var data = ["A", "B", "C", "D", "E", "F", "G", "H", "I" ]; // corrected Array syntax
var table = document.createElement("table");
var i = 0;
for (var r = 0; r < 3; r++) {
var row = table.insertRow(-1);
for (var c = 0; c < 3; c++) {
var cell = row.insertCell(-1);
cell.appendChild(document.createTextNode(data[i++]));
}
}
document.body.appendChild(table);
var data = ["A", "B", "C", "D", "E", "F", "G", "H", "I" ];
var html = '<table><tr>';
for(var i = 0 ; i<data.length; i++){
if(i%3 == 0 && i != 0){
html+= '</tr><tr>'
}
html+='<td>'+data[i]+'</td>';
}
html+='</tr></table>';
document.write(html);
http://jsfiddle.net/DuSP5/ just tried this one.
精彩评论