Columngroup span 属性
返回 <colgroup> 元素横跨的列数:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<colgroup id="myColgroup" span="2" style="background:red"></colgroup>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>3476896</td>
<td>我的第一个 HTML</td>
<td>$53</td>
</tr>
<tr>
<td>5869207</td>
<td>我的第一个 CSS</td>
<td>$49</td>
</tr>
</table>
<p>点击按钮返回colgroup元素span属性的值。</p>
<p id="demo"></p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var x = document.getElementById("myColgroup").span;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
定义和使用
span 属性设置或者返回列组span属性的值。
span 属性定义 <colgroup> 元素横跨的列数。
提示: 描述 <colgroup>中不同列元素的属性, 请使用 Column 对象。
浏览器支持
![]()
![]()
![]()
![]()
![]()
所有主流浏览器都支持 span 属性。
语法
返回 span 属性:
columngroupObject.span
设置 formMethod 属性:
columngroupObject.span=number
属性值
| 值 | 描述 |
|---|---|
| number | 描述了<colgroup> 元素中横跨的列数。 |
技术描述
| 返回值: | 返回列数 |
|---|
更多实例
使用 span 和 backgroundColor 属性设置前两列的背景颜色为红色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<colgroup id="myColgroup"></colgroup>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>3476896</td>
<td>My first HTML</td>
<td>$53</td>
</tr>
<tr>
<td>5869207</td>
<td>My first CSS</td>
<td>$49</td>
</tr>
</table>
<p>点击按钮设置表格中前两列的颜色为红色</p>
<p id="demo"></p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
document.getElementById("myColgroup").span = "2";
document.getElementById("myColgroup").style.backgroundColor = "red";
}
</script>
</body>
</html>
相关页面
HTML 参考手册: HTML <colgroup> span 属性
Columngroup 对象
Columngroup 对象