คุณสมบัติ span ของ HTML DOM ColumnGroup เชื่อมโยงกับแอตทริบิวต์ span ขององค์ประกอบ HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์สำหรับ −
การตั้งค่าคุณสมบัติช่วง ColumnGroup -
columngroupObject.span = number
ในที่นี้ ตัวเลขจะระบุจำนวนคอลัมน์ที่องค์ประกอบ
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับคุณสมบัติช่วงของ ColumnGroup -
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid blue;
}
</style>
</head>
<body>
<table>
<colgroup id="Colgroup1"></colgroup>
<tr>
<th>Fruit</th>
<th>COLOR</th>
<th>Price</th>
</tr>
<tr>
<td>watermelon</td>
<td>dark green</td>
<td>40Rs</td>
</tr>
<tr>
<td>papaya</td>
<td>yellow</td>
<td>30Rs</td>
</tr>
</table>
<p>lick the button to change the background color of the first two columns.
<button onclick="changeColor()">CHANGE</button>
<script>
function changeColor() {
document.getElementById("Colgroup1").span = "2";
document.getElementById("Colgroup1").style.backgroundColor = "lightgreen";
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

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

ในตัวอย่างข้างต้น −
เราได้สร้างตารางที่มีสองแถวและสามคอลัมน์ นอกจากนี้ยังมีการจัดรูปแบบที่ใช้กับองค์ประกอบตาราง th และ td -
table, th, td {
border: 1px solid blue;
}
<table>
<colgroup id="Colgroup1"></colgroup>
<tr>
<th>Fruit</th>
<th>COLOR</th>
<th>Price</th>
</tr>
<tr>
<td>watermelon</td>
<td>dark green</td>
<td>40Rs</td>
</tr>
<tr>
<td>papaya</td>
<td>yellow</td>
<td>30Rs</td>
</tr>
</table> จากนั้นเราได้สร้างปุ่ม CHANGE ที่จะรันเมธอด changeColor() เมื่อผู้ใช้คลิก
<button onclick="changeColor()">CHANGE</button>
ฟังก์ชัน changeColor() รับองค์ประกอบ
function changeColor() {
document.getElementById("Colgroup1").span = "2";
document.getElementById("Colgroup1").style.backgroundColor = "lightgreen";
}