您的位置:首页 > Web前端 > JavaScript

Javascript实例教程:DOM方法创建和修改表格

2013-10-12 13:22 627 查看
<table>元素是HTML中最复杂的结构之一。要想创建表格,一般都必须涉及表示表格行、单元格、表头等方面的标签。由于涉及的标签多,因而使用核心DOM方法创建和修改表格往往都免不了要编写大量的代码。假设我们要使用DOM来创建下面的HTML表格:

<table border="1" width="100%">
<tbody>
<tr>
<td>Cell 1,1</td>
<td>Cell 2,1</td>
</tr>
<tr>
<td>Cell 1,2</td>
<td>Cell 2,2</td>
</tr>
</tbody>
</table>

要是用核心DOM方法创建这些元素,得需要像下面这么多的代码:

//创建table
var table = document.createElement("table");
table.border = 1;
table.width = "100%";

//创建tbody
var tbody = document.createElement("tbody");
table.appendChild(tbody);

//创建第一行
var row1 = document.createElement("tr");
tbody.appendChild(row1);
var cell1_1 = document.createElement("td");
cell1_1.appendChild(document.createTextNode("Cell 1,1"));
row1.appendChild(cell1_1);
var cell2_1 = document.createElement("td");
cell2_1.appendChild(document.createTextNode("Cell 2,1"));
row1.appendChild(cell2_1);

//创建第二行
var row2 = document.createElement("tr");
tbody.appendChild(row2);
var cell1_2 = document.createElement("td");
cell1_2.appendChild(document.createTextNode("Cell 1,2"));
row2.appendChild(cell1_2);
var cell2_2 = document.createElement("td");
cell2_2.appendChild(document.createTextNode("Cell 2,2"));
row2.appendChild(cell2_2);

//将表格插入到文档主题中
document.body.appendChild(table);

显然,DOM代码很长,还有点不太好懂。为了方便构建表格,HTML DOM 还为<table>、<tbody>和<tr>元素添加了一些属性和方法。

为<table>元素添加的属性和方法如下:

caption:保存这对<caption>元素(如果有)的指针;
tBodies:是一个<tbody>元素的HTMLCollection;
tFoot:保存着对<tfoot>元素(如果有)的指针;
tHead:保存着对<thead>元素(如果有)的指针
rows:是一个表格中所有行的HTMLCollection;
createTHead():创建<thead>元素,将其放到表格中,返回引用;
createTFoot():创建<tfoot>元素,将其放到表格中,返回引用;
createCaption();创建<caption>元素,将其放到表格中,返回引用;
deleteTHead();删除<thead>元素;
deleteTFoot();删除<tfoot>元素;
deleteCaption():删除<caption>元素;
deleteRow(pos):删除指定位置的行;
insertRow(pos):向rows集合中的指定位置插入一行。

为<tbody>元素添加属性和方法有:

rows:保存着<tbody>元素中行的HTMLCollection;
deleteRow(pos):删除制定位置的行;
insertRow(pos):向rows集合中的指定位置插入一行,返回对新插入行的引用。

为<tr>元素添加的属性和方法如下:

cells:保存着<tr>元素中单元格的HTMLCollection;
deleteCell(pos):删除指定位置的单元格;
insertCell(pos):向cells集合中的指定位置插入一个单元格,返回新插入单元格的引用。

使用这些属性和方法,可以极大的减少创建报个所需要的代码数量。例如,使用这些属性和方法可以将前面的代码重写如下:

//创建table
var table = document.createElement("table");
table.border = 1;
table.width = "100%";

//创建tbody
var tbody = document.createElement("tbody");
table.appendChild(tbody);
tbody.insertRow(0);
tbody.rows[0].insertCell(0);
tbody.rows[0].cells[0].appendChild(document.createTextNode("Cell 1,1"));
tbody.rows[0].insertCell(1);
tbody.rows[0].cells[1].appendChild(document.createTextNode("Cell 2,1"));

//创建第一行
tbody.insertRow(1);
tbody.rows[1].insertCell(0);
tbody.rows[1].cells[0].appendChild(document.createTextNode("Cell 1,2"));
tbody.rows[1].insertCell(1);
tbody.rows[1].cells[1].appendChild(document.createTextNode("Cell 2,2"));

//将表格添加到文档主题中
document.body.appendChild(table);

在这次代码中,创建<table>和<tbody>的代码没有变化。不同是创建两行的部分,其中使用了HTML DOM定义的表格属性和方法。在创建第一行时,通过<tbody>元素调用了insertRow()方法,传入了参数0——表示应该插入的行放在什么位置上。执行这一样的代码后,就会自动创建一行并将其插入到<tbody>元素的位置0上。因此马上就可以通过tbody.rows[0]来引用新插入的行。

创建单元格的方式也十分相似,即通过<tr>元素调用insertCell()方法并传入放置单元格的位置。然后,就可以通过tbody.rows[0].cells[0]来引用新插入的单元格,因为新创建的单元格被插入到了这一行的位置0上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: