JavaScript操作表格代码

来源:互联网 发布:js自动答题代码 编辑:程序博客网 时间:2024/06/05 22:54

  Cell 1,1  Cell 2,1

  Cell 1,2  Cell 2,2

  要是用核心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 还为、和元素添加了一些属性和方法。

  为

  元素添加的属性和方法如下:

  caption:保存这对元素(如果有)的指针;

  tBodies:是一个元素的HTMLCollection;

  tFoot:保存着对元素(如果有)的指针;

  tHead:保存着对元素(如果有)的指针

  rows:是一个表格中所有行的HTMLCollection;

  createTHead():创建元素,将其放到表格中,返回引用;

  createTFoot():创建元素,将其放到表格中,返回引用;

  createCaption();创建元素,将其放到表格中,返回引用;

  deleteTHead();删除元素;

  deleteTFoot();删除元素;

  deleteCaption():删除元素;

  deleteRow(pos):删除指定位置的行;

  insertRow(pos):向rows集合中的指定位置插入一行。

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

  rows:保存着元素中行的HTMLCollection;

  deleteRow(pos):删除制定位置的行;

  insertRow(pos):向rows集合中的指定位置插入一行,返回对新插入行的引用。

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

  cells:保存着

  元素中单元格的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);

  zhan.renren.com/asdfas?gid=3602888498049757508&checked=true

  zhan.renren.com/asdfas?gid=3602888498049757527&checked=true

  zhan.renren.com/asdfas?gid=3602888498049757508

  zhan.renren.com/asdfas?gid=3602888498049757527

  zhan.renren.com/asdfas?gid=3602888498049757508&from=post

  zhan.renren.com/asdfas?gid=3602888498049757527&from=post

  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);

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

  创建单元格的方式也十分相似,即通过元素调用insertCell()方法并传入放置单元格的位置。然后,就可以通过tbody.rows[0].cells[0]来引用新插入的单元格,因为新创建的单元格被插入到了这一行的位置0上。

  大家还都关注: JavaScript教程

0 0