คุณสมบัติ rows
คุณสมบัติ rows เก็บคอลเลกชันของแถว
tr
สามารถใช้ได้ทั้งกับตารางและกับ
ส่วนต่างๆ ของมัน เช่น tHead,
tBodies,
tFoot
ไวยากรณ์
ตาราง.rows;
ตัวอย่าง
ลองวนลูปผ่านทุกแถวของตาราง:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
for (let row of table.rows) {
console.log(row);
}
ตัวอย่าง
ลองวนลูปผ่านทุกแถวของตารางโดยใช้
คุณสมบัติ rows และในแต่ละแถวให้วนลูปผ่าน
เซลล์ของมันโดยใช้คุณสมบัติ cells:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
for (let row of table.rows) {
for (let cell of row.cells) {
console.log(cell);
}
}
ตัวอย่าง
ลองหาจำนวนแถวของตาราง:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
console.log(table.rows.length);
ผลลัพธ์จากการรันโค้ด:
3