ออบเจ็กต์ HTML DOM TableRow แสดงถึงองค์ประกอบ
สร้างวัตถุ TableRow
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
document.createElement(“TR”);
คุณสมบัติของวัตถุ TableRow
| ทรัพย์สิน | คำอธิบาย |
|---|---|
| rowIndex | ส่งกลับตำแหน่งของแถวในคอลเลกชันแถวของตาราง |
| sectionRowIndex | ส่งกลับตำแหน่งของแถวในคอลเลกชั่นแถวของ thead, tbody หรือ tfoot |
วิธีการของวัตถุ TableRow
| วิธีการ | คำอธิบาย |
|---|---|
| deleteCell() | ลบเซลล์ออกจากแถวตารางปัจจุบัน |
| insertCell() | จะเพิ่มเซลล์ลงในแถวของตารางปัจจุบัน |
ให้เราดูตัวอย่างของวัตถุ TableRow:
ตัวอย่าง
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
text-align: center;
}
table {
margin: 2rem auto;
width: 400px;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem auto;
}
</style>
<body>
<h1>DOM TableRow Object Demo</h1>
<table border="2">
<thead>
<tr>
<th>Name</th>
<th>Language</th>
</tr>
<thead>
<tbody class="table-body">
<tr>
<td>John</td>
<td>English</td>
</tr>
<tr>
<td>Elon</td>
<td>Germany</td>
</tr>
</tbody>
</table>
<button onclick="get()" class="btn">Create TableRow</button>
<script>
function get() {
var tr = document.createElement("TR");
tr.innerHTML = "<td>Mario</td><td>French</td>"
document.querySelector(".table-body").appendChild(tr);
}
</script>
</body>
</html> ผลลัพธ์

คลิกที่ “สร้าง TableRow” เพื่อสร้างแถวของตาราง
