您已经在 Select 现有表:
const tbl = document.getElementById("table")
因为你称之为:
document.body.appendChild(tbl) // Remove this
您正在将桌子移动到主体底部,位于按钮之后.
除非您年满actually岁,创建这样的表格:
const tbl = document.createElement("table")
不需要附加它,也不需要移动它在多姆中的位置.
工作片段
这是更新后的片段.我重新命名了一些变量,将其称为append
而不是appendChild
,并使用textContent
来简化您的逻辑.
createTable(10, 5, ["Ann", "Joe", "Mike", "Sam"]);
function createTable(rowCount, colCount, playerNames) {
const table = document.querySelector("#grid");
let tbody = table.querySelector("tbody");
if (tbody == null) {
tbody = document.createElement("tbody");
table.append(tbody);
}
// Populate the table with data
for (let rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
const tr = document.createElement("tr");
for (let colIndex = 0; colIndex < colCount; colIndex++) {
const td = document.createElement("td");
if (rowIndex == 0 && colIndex == 0) {
td.textContent = "Round";
} else if (rowIndex == 0 && colIndex != 0) {
// Create cells with player names in first row
td.textContent = playerNames[colIndex - 1];
} else if (rowIndex != 0 && colIndex == 0) {
// Create round counter cells
td.textContent = rowIndex;
} else {
// Create rest of the cells
td.contentEditable = true;
}
tr.append(td);
}
tbody.append(tr);
}
addSumRowToTable(tbody, colCount);
addEventListenerToTableDataCells();
displayEventCardsButton();
}
function addSumRowToTable(tbody, colCount) {
const tr = document.createElement("tr");
for (let colIndex = 0; colIndex < colCount; colIndex++) {
const td = document.createElement("td");
if (colIndex === 0) {
td.textContent = "Sum";
} else {
td.textContent = "$";
}
tr.append(td);
}
tbody.append(tr);
}
function addEventListenerToTableDataCells() {
// Not implemented
}
function displayEventCardsButton() {
let eventCardButton = document.getElementById("event_cards");
if (eventCardButton) {
eventCardButton.style.display = "block";
}
}
table#grid {
border-collapse: collapse;
}
table#grid, table#grid td {
border: thin solid grey;
}
table#grid td {
padding: 0.25rem;
}
table#grid tr td:first-child {
text-align: right;
}
<div id="player_count_heading">Anzahl der Spieler eingeben</div>
<div>
<table id="grid"></table>
</div>
<div class="buttons">
<button id="player_count" type="button">Spieleranzahl</button>
<button id="event_cards" type="button">Ereigniskarten</button>
</div>
<script src="js/frantic.js"></script>
更好的方式
不过,这是一个更好的方法.您还应该利用<thead>
、<tfoot>
和<th>
个元素.此外,如果您的函数名为"SEARCH X",则它可能应该创建并返回一些内容.
const existingTable = document.querySelector("#grid");
const table = createTable(["Ann", "Joe", "Mike", "Sam"], 10);
table.id = existingTable.id;
existingTable.replaceWith(table);
function createTable(playerNames, rounds) {
const table = document.createElement("table");
const thead = document.createElement("thead");
const tbody = document.createElement("tbody");
const tfoot = document.createElement("tfoot");
const colCount = playerNames.length + 1;
const headerRow = document.createElement("tr");
for (let colIndex = 0; colIndex < colCount; colIndex++) {
const header = document.createElement("th");
header.textContent = colIndex === 0 ? "Round" : playerNames[colIndex - 1];
headerRow.append(header);
}
thead.append(headerRow);
// Populate the table with data
for (let rowIndex = 0; rowIndex < rounds; rowIndex++) {
const tr = document.createElement("tr");
for (let colIndex = 0; colIndex < colCount; colIndex++) {
if (colIndex === 0) {
// Create round counter cells
const th = document.createElement("th");
th.textContent = rowIndex + 1;
tr.append(th);
} else {
// Create rest of the cells
const td = document.createElement("td");
td.contentEditable = true;
tr.append(td);
}
}
tbody.append(tr);
}
table.append(thead);
table.append(tbody);
table.append(tfoot);
addSumRowToTable(tfoot, playerNames.length + 1);
addEventListenerToTableDataCells();
displayEventCardsButton();
return table;
}
function addSumRowToTable(tfoot, colCount) {
const tr = document.createElement("tr");
for (let colIndex = 0; colIndex < colCount; colIndex++) {
if (colIndex === 0) {
// Create round counter cells
const th = document.createElement("th");
th.textContent = "Sum";
tr.append(th);
} else {
// Create rest of the cells
const td = document.createElement("td");
td.textContent = "$";
tr.append(td);
}
}
tfoot.append(tr);
}
function addEventListenerToTableDataCells() {
// Not implemented
}
function displayEventCardsButton() {
let eventCardButton = document.getElementById("event_cards");
if (eventCardButton) {
eventCardButton.style.display = "block";
}
}
table#grid {
border-collapse: collapse;
}
table#grid, table#grid th, table#grid td {
border: thin solid grey;
}
table#grid th, table#grid td {
padding: 0.25rem;
}
table#grid tbody tr th:first-child {
text-align: right;
background: #EEE;
}
table#grid thead {
background: #CCC;
}
table#grid tfoot {
background: #EEE;
}
<div id="player_count_heading">Anzahl der Spieler eingeben</div>
<div>
<table id="grid"></table>
</div>
<div class="buttons">
<button id="player_count" type="button">Spieleranzahl</button>
<button id="event_cards" type="button">Ereigniskarten</button>
</div>
<script src="js/frantic.js"></script>