使用香草 JS 和循环绘制表格
Drawing a table with vanilla JS and loops
我正在做一个练习(从开始Javascript)来更好地理解DOM操作。尝试仅使用 JS 在 DRY 方法中重新创建下表(教科书解决方案在这里):
<table>
<tr>
<td>Car</td>
<td>Top Speed</td>
<td>Price</td>
</tr>
<tr>
<td>Chevrolet</td>
<td>120mph</td>
<td>$10,000</td>
</tr>
<tr>
<td>Pontiac</td>
<td>140mph</td>
<td>$20,000</td>
</tr>
</table>
我试过这个,但不确定如何在不抛出错误的情况下循环创建变量:
var array = [['Car', 'Top Speed', 'Price'],['Chevrolet', '120mph', '$10,000'], ['Pontiac', '140pmh', '$20,000']] // Creating a data array which a loop will source from
var table = document.createElement('table');
document.body.appendChild(table); // Drew the main table node on the document
for (var i = 0; i<3; i++) {
var tr[i] = document.createElement('tr'); //Create 3 <tr> elements assigned to a unique variable BUT need a working alternative for 'tr[i]'
table.appendChild(tr[i]); // Append to <table> node
for (var j = 0; j<3; j++) {
var tdText = document.createTextNode(array[i][j]); // Extract data from array to a placeholder variable
tr[i].appendChild(tdText); // Take string from placeholder variable and append it to <tr> node
}
}
如前所述,问题是声明tr[i]
变量时的语法错误。
更干净的方法是使用表 api 方法,例如
var array = [
['Car', 'Top Speed', 'Price'],
['Chevrolet', '120mph', '$10,000'],
['Pontiac', '140pmh', '$20,000']
] // Creating a data array which a loop will source from
var table = document.createElement('table');
document.body.appendChild(table); // Drew the main table node on the document
array.forEach(function(row) {
var tr = table.insertRow(); //Create a new row
row.forEach(function(column) {
var td = tr.insertCell();
td.innerText = column; // Take string from placeholder variable and append it to <tr> node
});
});
- HTMLTableElement
- 插入行()
- 插入单元格
请使用 tr
而不是 tr[i]
。它会工作
var array = [['Car', 'Top Speed', 'Price'],['Chevrolet', '120mph', '$10,000'], ['Pontiac', '140pmh', '$20,000']] // Creating a data array which a loop will source from
var table = document.createElement('table');
document.body.appendChild(table); // Drew the main table node on the document
for (var i = 0; i<3; i++) {
var tr = document.createElement('tr'); //Create 3 <tr> elements assigned to a unique variable BUT need a working alternative for 'tr[i]'
table.appendChild(tr); // Append to <table> node
for (var j = 0; j<3; j++) {
var tdElement = document.createElement('td');
tdElement.innerHTML = array[i][j];
tr.appendChild(tdElement); // Take string from placeholder variable and append it to <tr> node
}
}
我的版本使用THead
和TBody
以及th
和td
。
// input as an array
const array = [
['Car', 'Top Speed', 'Price'],
['Chevrolet', '120mph', '$10,000'],
['Pontiac', '140pmh', '$20,000']
]
const table = document.createElement('table');
// Generate Header
const head = table.createTHead();
const tr = head.insertRow();
array[0].forEach(function(item) {
const th = document.createElement('th')
th.appendChild(document.createTextNode(item));
tr.appendChild(th);
});
// Generate Body
const body = table.createTBody();
array.slice(1).forEach(function(row) {
const tr = body.insertRow();
row.forEach(function(item, index) {
if (index === 0) {
const th = document.createElement('th')
th.appendChild(document.createTextNode(item));
tr.appendChild(th);
} else {
const td = tr.insertCell(); // create td only
td.appendChild(document.createTextNode(item));
}
});
});
document.body.appendChild(table);
裁判:
-
<thead>
: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead -
<tbody>
: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tbody -
<th>
: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th
相关文章:
- Javascript:延迟在html5画布上循环绘制
- 使用javascript中的for循环在画布上绘制路径
- 从带有循环的数组中一个接一个地在画布上绘制形状
- 使用 for 循环在画布中绘制 img
- 使用香草 JS 和循环绘制表格
- 使用 for 循环绘制 HTML5 画布圆圈
- Javascript,如何加载,绘制和循环120帧
- 如何使用 svg 循环绘制贝塞尔曲线
- 使用 for 循环动态创建在画布内绘制的图像
- 在绘制图表之前在每个循环中编排嵌套的 AJAX 调用 (JQuery)
- 如何使用延迟的 for 循环绘制画布
- 画布用循环绘制线条
- HTML 5 画布:在游戏中使用绘制图像会使循环非常慢
- 使用for循环在谷歌地图上绘制饼图效果不佳
- 如何在JavaScript/JQuery中绘制循环图
- 使用js-op和svg绘制循环
- MySql和PHP循环使用x,y坐标绘制正方形
- 使用视频循环绘制画布
- 在用html画布和javascript绘制塞舌尔国旗后,我试图使用数组或循环来简化我的代码
- 如何在D3中使用循环绘制许多函数