JavaScript入门(五)操作表格

本文介绍了一个使用DOM(文档对象模型)来创建和操作HTML表格的具体示例。通过JavaScript代码动态生成了一个包含标题、表头、主体及表尾的表格,并展示了如何设置表格的基本属性如宽度和边框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
<title>DOM文档对象模型 --操作表格和样式</title>

<script>
//thead, tfoot 只能有一个
//tbody可以有多个

//使用DOM创建表格

window.onload=function(){
var table = document.createElement('table');
table.width=300;//table.setAttribute('width',300);
table.border=1;
var caption=document.createElement('caption');
table.appendChild(caption);
caption.innerHTML='人员表';
var captionText=document.createTextNode('人员表');
caption.appendChild(captionText);

var thead=document.createElement('thead');
table.appendChild(thead);

var tr=document.createElement('tr');
thead.appendChild(tr);

var th=document.createElement('th');
tr.appendChild(th);

th.appendChild(document.createTextNode("数据1"));
th.appendChild(document.createTextNode("数据2"));


document.body.appendChild(table);
}

</script>

</head>

<body >
	<table border='1' width='300'>
		<caption>人员表</caption>
		<thead>
			<tr>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<th>张三</th>
				<th>男</th>
				<th>22</th>
			</tr>
			<tr>
				<th>李四</th>
				<th>男</th>
				<th>22</th>
			</tr>
		</tbody>
				<tbody>
			<tr>
				<th>王五</th>
				<th>男</th>
				<th>22</th>
			</tr>
			<tr>
				<th>人六</th>
				<th>男</th>
				<th>22</th>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan='3'>合计:2人</td>
			</tr>
		</tfoot>

	</table>


</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多则惑少则明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值