2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
invoiceTableHTML 是相同的,但如果您愿意,可以刪除id 。
JS:
function addRow() {
var tbody = document.querySelector("tbody")
tbody.innerHTML +=
'<tr>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>'; +
'</tr>';
}
因?yàn)槲覜](méi)有看到循環(huán)每個(gè)元素的意義,所以我只是直接擴(kuò)展了新行。+ 按鈕仍會(huì)在最后創(chuàng)建一行,但其他任何操作都不會(huì)。這是想要的嗎?

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
table.rows.length將選擇所有內(nèi)容rows,包括具有 的行th。相反,僅選擇tbody trs.
function addRow() {
var table = document.querySelector("tbody"),
rIndex, cIndex;
for (var i = 0; i < table.rows.length; i++) {
// console.log(table.rows[i].cells);
for (var j = 0; j < table.rows[i].cells.length; j++) {
table.rows[i].cells[j].onclick = function() {
rIndex = this.parentElement.rowIndex + 1;
cIndex = this.cellindex + 1;
document.getElementById("invoiceTable").insertRow(rIndex).innerHTML = '' +
'<tr>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div contenteditable></div></td>' +
'<td><div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i class="fas fa-plus-circle"></i></button></div></td>'; +
'</tr>'
}
}
}
}
<link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet"/>
<main role="main">
<div class="container">
<div class="row">
<div class="col-md-12 mt-5">
<table class="table thead-dark table-hover border-bottom" id="invoiceTable">
<thead>
<tr>
<th style="width: 10%">Item</th>
<th style="width: 50%">Description</th>
<th style="width: 5%">Qty</th>
<th style="width: 10%">Price</th>
<th style="width: 10%">Amount</th>
<th style=>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div contenteditable></div>
</td>
<td>
<div contenteditable></div>
</td>
<td>
<div contenteditable></div>
</td>
<td>
<div contenteditable></div>
</td>
<td>
<div contenteditable></div>
</td>
<td>
<div><button type="button" name="button" onclick="addRow()" style="background-color:Transparent; border:none; color:green;"><i
class="fas fa-plus-circle"></i></button></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</main>
<!-- /role main -->
- 2 回答
- 0 關(guān)注
- 185 瀏覽
添加回答
舉報(bào)