Baseline Widely available
HTMLTableSectionElement
æ¥å£ç deleteRow()
æ¹æ³ä»ç»å®ç <section>
ä¸ç§»é¤ç¹å®çè¡ï¼<tr>
ï¼ã
index
index
æ¯ä¸ä¸ªè¡¨ç¤ºåºè¯¥å é¤è¡çæ´æ°ãç¶èï¼ç¹æ®ç´¢å¼ -1
å¯ä»¥ç¨äºå é¤å½ååæ®µçæåä¸è¡ã
æ ï¼undefined
ï¼ã
IndexSizeError
DOMException
妿 index
大äºçäºè¡æ°æå°äº -1
ï¼åæåºè¯¥å¼å¸¸ã
å¨è¿ä¸ªç¤ºä¾ä¸ï¼æä¸¤ä¸ªæé®å
è®¸ä½ å¯¹è¡¨æ ¼ä¸»ä½æ·»å åç§»é¤è¡ï¼å®è¿ä½¿ç¨è¡¨ä¸å½åè¡æ°æ´æ° <output>
å
ç´ ã
<table>
<thead>
<th>å 1</th>
<th>å 2</th>
<th>å 3</th>
</thead>
<tbody>
<tr>
<td>X</td>
<td>Y</td>
<td>Z</td>
</tr>
</tbody>
</table>
<button id="add">æ·»å ä¸è¡</button>
<button id="remove">ç§»é¤æåä¸è¡</button>
<div>è¡¨æ ¼ä¸»ä½æ <output>1</output> è¡ã</div>
table {
border-collapse: collapse;
}
th,
td {
border: 1px solid black;
}
button {
margin: 1em 1em 1em 0;
}
JavaScript
// è·åç¸å
³æ¥å£å
ç´
const bodySection = document.querySelectorAll("tbody")[0];
const rows = bodySection.rows; // é忝卿çï¼å æ¤å
¶æ»æ¯ææ°ç
const rowNumberDisplay = document.querySelectorAll("output")[0];
const addButton = document.getElementById("add");
const removeButton = document.getElementById("remove");
function updateRowNumber() {
rowNumberDisplay.textContent = rows.length;
}
addButton.addEventListener("click", () => {
// å¨ä¸»ä½çæ«å°¾æ·»å ä¸ä¸ªæ°è¡
const newRow = bodySection.insertRow();
// 卿°è¡å
æ·»å åå
æ ¼
["A", "B", "C"].forEach(
(elt) => (newRow.insertCell().textContent = `${elt}${rows.length}`),
);
// æ´æ°è¡è®¡æ°
updateRowNumber();
});
removeButton.addEventListener("click", () => {
// ä»ä¸»ä½å é¤è¡
bodySection.deleteRow(-1);
// æ´æ°è¡è®¡æ°
updateRowNumber();
});
ç»æ è§è æµè§å¨å
¼å®¹æ§ åè§
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