Baseline Widely available
HTMLTableRowElement
æ¥å£ç insertCell()
æ¹æ³å°ä¸ä¸ªæ°åå
æ ¼ï¼<td>
ï¼æå
¥å°è¡¨è¡ï¼<tr>
ï¼ä¸ï¼ç¶åè¿åæ°åå
æ ¼çå¼ç¨ã
夿³¨ï¼ insertCell()
å°åå
æ ¼ç´æ¥æå
¥è¡ä¸ï¼å¦æä½¿ç¨ Document.createElement()
å建æ°ç <td>
å
ç´ ï¼åä¸éè¦ä½¿ç¨ Node.appendChild()
åç¬è¿½å åå
æ ¼ã
ä½ ä¸è½ä½¿ç¨ insertCell()
æ¥å建ä¸ä¸ªæ°ç <th>
å
ç´ ã
insertCell()
insertCell(index)
åæ°
index
å¯é
æ°åå
æ ¼çåå
æ ¼ç´¢å¼ï¼å¦æ index
æ¯ -1
æè
çäºåå
æ ¼æ°ï¼æ°åå
æ ¼éå å°è¡çæåä¸ä¸ªåå
æ ¼ã妿çç¥ index
ï¼åé»è®¤å¼æ¯ -1
ã
ä¸ä¸ªå¼ç¨æ°åå
æ ¼ç HTMLTableCellElement
ã
IndexSizeError
DOMException
妿 index
大äºåå
æ ¼æ°ï¼åæåºæ¤å¼å¸¸ã
æ¤ç¤ºä¾ä½¿ç¨ HTMLTableRowElement.insertCell()
å°æ°çåå
æ ¼éå å°è¡ä¸ã
<table>
<thead>
<tr>
<th>C1</th>
<th>C2</th>
<th>C3</th>
<th>C4</th>
<th>C5</th>
</tr>
</thead>
<tbody>
<tr>
<td>åå
æ ¼ 1</td>
<td>åå
æ ¼ 2</td>
</tr>
</tbody>
</table>
<button id="add">æ·»å åå
æ ¼</button>
<button id="remove">ç§»é¤æåçåå
æ ¼</button>
<div>第ä¸è¡æ <output>2</output> 个åå
æ ¼ã</div>
table {
border-collapse: collapse;
}
th,
td,
table {
border: 1px solid black;
}
button {
margin: 1em 1em 1em 0;
}
JavaScript
// è·åç¸å
³æ¥å£å
ç´
const bodySection = document.querySelectorAll("tbody")[0];
const row = bodySection.rows[0]; // éæ©è¡¨æ ¼ä½é¨åç第ä¸è¡
const cells = row.cells; // é忝卿çï¼å æ¤æ»æ¯ææ°ç
const cellNumberDisplay = document.querySelectorAll("output")[0];
const addButton = document.getElementById("add");
const removeButton = document.getElementById("remove");
function updateCellNumber() {
cellNumberDisplay.textContent = cells.length;
}
addButton.addEventListener("click", () => {
// å¨ç¬¬ä¸è¡çæ«å°¾æ·»å åå
æ ¼
const newCell = row.insertCell();
newCell.textContent = `åå
æ ¼ ${cells.length}`;
// æ´æ°è¡æ°
updateCellNumber();
});
removeButton.addEventListener("click", () => {
// ä»è¡¨æ ¼ä½å é¤è¡
row.deleteCell(-1);
// æ´æ°è¡æ°
updateCellNumber();
});
ç»æ è§è æµè§å¨å
¼å®¹æ§ åè§
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4