To facilitate building tables, the HTML Dom adds properties and methods to the <table>, <tbody>, and <tr> elements.
The properties and methods added for the <table> element are as follows.
- Caption: holds a pointer to the <caption> element (if any).
- Tbodies: is a <tbody> element of the htmlcollection.
- TFoot: Holds a pointer to the <tfoot> element (if any).
- THead: Holds a pointer to the <thead> element (if any).
- Rows: Is the htmlcollection of all rows in a table.
- Createthead (): Creates a <thead> element, places it in a table, and returns a reference.
- Createtfoot (): Creates a <tfoot> element, places it in a table, and returns a reference.
- Createcaption (): Creates a <caption> element, places it in a table, and returns a reference.
- Deletethead (): Delete <thead> element.
- Deletetfoot (): Delete <tfoot> element.
- Deletecaption (): Delete <caption> element.
- DeleteRows (pos): Deletes the row at the specified location.
- InsertRows (pos): Inserts a row into the rows collection at the specified location.
The properties and methods added for the <tbody> element are as follows.
- Rows: Holds the htmlcollection of the rows in the <tbody> element.
- DeleteRows (pos): Deletes the row at the specified location.
- InsertRows (pos): Inserts a row into the rows collection at the specified location, returning a reference to the newly inserted.
The properties and methods added for the <tr> element are as follows.
- Cells: Holds the htmlcollection of the cells in the <tr> element.
- Deletecell (POS): Deletes the cell at the specified location.
- InsertCell (POS): Inserts a cell into the cells collection at the specified location, returning a reference to the newly inserted cell.
Using these properties and methods can greatly reduce the amount of code required to create a table. For example:
1 //CREATE TABLE2 varTable = document.createelement ("table");3Table.border = 1;4Table.width = "100%";5 6 //Create Tbody7 varTbody = document.createelement ("Tbody");8 Table.appendchild (tbody);9 Ten //Create first row OneTbody.insertrow (0); ATbody.rows[0].insertcell (0); -Tbody.rows[0].cells[0].appendchild (document.createTextNode ("Cell")); -Tbody.rows[0].insertcell (1); theTbody.rows[0].cells[1].appendchild (document.createTextNode ("Cell 2,1")); - - //Create second row -Tbody.insertrow (1); +Tbody.rows[1].insertcell (0); -Tbody.rows[1].cells[0].appendchild (document.createTextNode ("Cell")); +Tbody.rows[1].insertcell (1); ATbody.rows[1].cells[1].appendchild (document.createTextNode ("Cell 2,2")); at - //Add a table to the body of a document -Document.body.appendChild (table);
Dom Operation CREATE TABLE