假设想用dom来创建如下的html表格:
<table border="1" width="100%">
<tbody>
<tr>
<td>第一行第一列</td>
<td>第一行第二列</td>
</tr>
<tr>
<td>第二行第一列</td>
<td>第二行第二列</td>
</tr>
</tbody>
</table>
如果要通过核心dom来完成这个任务,你的代码有可能是下面这个样子:
<html>
<head>
<title>通过核心dom创建表格</title>
<script>
function append()
{
var tablenode = document.createElement("table");
tablenode.border = "1";
tablenode.width = "100%";
var tbodynode = document.createElement("tbody");
tablenode.appendChild(tbodynode);
var trnode1 = document.createElement("tr");
tbodynode.appendChild(trnode1);
var tdnode11 = document.createElement("td");
tdnode11.appendChild(document.createTextNode("第一行第一列"));
trnode1.appendChild(tdnode11);
var tdnode12 = document.createElement("td");
tdnode12.appendChild(document.createTextNode("第一行第二列"));
trnode1.appendChild(tdnode12);
var trnode2 = document.createElement("tr");
tbodynode.appendChild(trnode2);
var tdnode21 = document.createElement("td");
tdnode21.appendChild(document.createTextNode("第二行第一列"));
trnode2.appendChild(tdnode21);
var tdnode22 = document.createElement("td");
tdnode22.appendChild(document.createTextNode("第二行第二列"));
trnode2.appendChild(tdnode22);
document.body.appendChild(tablenode);
}
</script>
</head>
<body>
<input type="button" value="添加" onclick="append()" >
</body>
</html>
这段代码十分的冗长而且有些难于理解。为了协助建立表格,html dom给table、tbody、tr等元素添加了一些属性和方法:
与table相关的:
caption指向<caption />元素;
tBodies指向<tbody />元素的集合;
tFoot指向<tfoot />元素;
tHead指向<thead />元素;
rows指向<tr />的集合;
createTHead()创建<thead />元素并将其放入表格;
createTFoot()创建<tfoot />元素并将其放入表格;
createCpation()创建<caption />元素并将其放入表格;
deleteTHead()删除<thead />元素;
deleteTFoot()删除<tfoot />元素;
deleteCaption()删除<cpation />元素;
deleteRow(pos)删除指定位置上的行;
insertRow(pos)在rows集合中的指定位置上插入一个新行;
与tbody相关的:
rows<tbody />中所有行的集合;
deleteRow(pos)删除指定位置上的行;
insertRow(pos)在rows集合中的指定位置上插入一个新行;
与tr相关的:
cells<tr />元素中所有单元格的集合;
deleteCell(pos)删除指定位置上的单元格;
insertCell(pos)在cells集合的给定位置上插入一个新的单元格;
上面的一切意味着使用这些简便的属性方法可以大大降低创建表格的复杂程度,最上面的任务就可以用如下代码来完成:
<html>
<head>
<title>通过核心dom创建表格</title>
<script>
function append()
{
var tablenode = document.createElement("table");
tablenode.border = "1";
tablenode.width = "100%";
var tbodynode = document.createElement("tbody");
tablenode.appendChild(tbodynode);
tbodynode.insertRow(0);
tbodynode.rows[0].insertCell(0);
tbodynode.rows[0].cells[0].appendChild(document.createTextNode("第一行第一列"));
tbodynode.rows[0].insertCell(1);
tbodynode.rows[0].cells[1].appendChild(document.createTextNode("第一行第二列"));
tbodynode.insertRow(1);
tbodynode.rows[1].insertCell(0);
tbodynode.rows[1].cells[0].appendChild(document.createTextNode("第二行第一列"));
tbodynode.rows[1].insertCell(1);
tbodynode.rows[1].cells[1].appendChild(document.createTextNode("第二行第二列"));
document.body.appendChild(tablenode);
}
</script>
</head>
<body>
<input type="button" value="添加" onclick="append()" >
</body>
</html>
把地址用手机拍下来,方便随时查看!
———— 马上加微信联系:15823808970 张老师 ————
沙坪坝校区:
报名联系人:张老师
联系电话:15823808970 023-65316279
联系QQ:869488371
联系地址:重庆市沙坪坝三峡广场广电大厦四楼(王府井旁边)
南坪校区:
报名联系人:张老师
联系电话:17723551922 023-62530792
联系QQ:332334275
联系地址:南坪万达广场2号写字楼6楼11号(南坪轻轨站2号出口右侧)
江北校区:
报名联系人:陈老师
联系电话:18983942570 023-63874645
联系QQ:255678862
联系地址:江北观音桥拓展大厦18楼(新世纪百货后面)
杨家坪校区:
报名联系人:高老师
联系电话:18983940174 023-68612501
联系QQ:1840862932
联系地址:杨家坪轻轨站旁聚彩阁10-4(九龙坡人民医院A区大门正对面)