ออบเจ็กต์คอลัมน์ HTML DOM เชื่อมโยงกับองค์ประกอบ HTML
| ทรัพย์สิน | คำอธิบาย |
|---|---|
| สแปน | การตั้งค่าหรือคืนค่าแอตทริบิวต์ span ของคอลัมน์ |
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์สำหรับ −
การสร้างวัตถุคอลัมน์ −
var a = document.createElement("COL"); ตัวอย่าง
เรามาดูตัวอย่างของคอลัมน์อ็อบเจ็กต์ −
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid blue;
}
#Col1{
background-color:pink;
}
</style>
</head>
<body>
<h3>COL OBJECT</h3>
<table>
<colgroup>
<col id="Col1" span="2">
<col style="background-color:lightgreen">
</colgroup>
<tr>
<th>Fruit</th>
<th>Color</th>
<th>Price</th>
</tr>
<tr>
<td>Mango</td>
<td>Yellow</td>
<td>100Rs</td>
</tr>
<tr>
<td>Guava</td>
<td>Green</td>
<td>50Rs</td>
</tr>
</table>
<p>Click the below button to get span of the "COL1" col element</p>
<button onclick="colObj()">COLUMN</button>
<p id="Sample"></p>
<script>
function colObj() {
var x = document.getElementById("Col1").span;
document.getElementById("Sample").innerHTML = "The Col1 element has span= "+x;
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่ม COLUMN -

ในตัวอย่างข้างต้น เราได้สร้างตารางที่มี 2 แถว 3 คอลัมน์ ตารางโดยรวมมีสไตล์ที่ใช้กับมัน ภายในตาราง เรามีองค์ประกอบ
table, th, td {
border: 1px solid blue;
}
#Col1{
background-color:pink;
}
<table>
<colgroup>
<col id="Col1" span="2">
<col style="background-color:lightgreen">
</colgroup>
<tr>
<th>Fruit</th>
<th>Color</th>
<th>Price</th>
</tr>
<tr>
<td>Mango</td>
<td>Yellow</td>
<td>100Rs</td>
</tr>
<tr>
<td>Guava</td>
<td>Green</td>
<td>50Rs</td>
</tr>
</table> เราได้สร้างปุ่ม COLUMN ที่จะรันเมธอด colObj() เมื่อผู้ใช้คลิก -
<button onclick="colObj()">COLUMN</button>
วิธี colObj() รับองค์ประกอบ
function colObj() {
var x = document.getElementById("Col1").span;
document.getElementById("Sample").innerHTML = "The Col1 element has span= "+x;
}