This article mainly summarizes the definitions and usage of tableinsertRow and deleteRow. For more information, see the following table: var trCnt = table. rows. length; (table is Id)
Each row has several columns: for (var I = 0; I Table. rows [I]. cells. length;
Javascript operation table:
InsertRow (), deleteRow (), insertCell (), deleteCell () method
Table. insertRow () is okay in IE, but you have to change it to table. insertRow (-1) in firefox)
Similarly, the corresponding insertCell () should be changed to insertCell (-1)
InsertRow () method
Definition and usage
The insertRow () method is used to Insert a new row at a specified position in the table.
Syntax
TableObject. insertRow (index)
Return Value
Returns a TableRow, indicating the newly inserted row.
Description
This method creates a new TableRow object, indicating a newAnd insert it into the specified position in the table.
The new row will be inserted before the row where the index is located. If index is equal to the number of rows in the table, the new row is appended to the end of the table.
If the table is empty, the new row is inserted into a newSegment, which is itself inserted into the table.
Throw
If the index is smaller than 0 or greater than or equal to the number of rows in the table, this method throws a DOMException code INDEX_SIZE_ERR.
Example
The Code is as follows:
<Head>
<Script type = "text/javascript">
Function insRow ()
{
Document. getElementById ('mytable'). insertRow (0)
}
</Script>
</Head>
<Body>
<Table id = "myTable" border = "1">
<Tr>
<Td> Row1 cell1
<Td> Row1 cell2
</Tr>
<Tr>
<Td> Row2 cell1
<Td> Row2 cell2
</Tr>
</Table>
<Br/>
<Input type = "button" onclick = "insRow ()"
Value = "Insert new row">
</Body>
</Html>
DeleteCell ()
Definition and usage
The deleteCell () method is used to delete cells (Element ).
Syntax
TablerowObject. deleteCell (index)
Description
The index parameter is the position of the table element to be deleted in the row.
This method deletes the table meta at the specified position in the table row.
Throw
If the index parameter is smaller than 0 or greater than or equal to the number of table elements in the row, this method throws a DOMException code INDEX_SIZE_ERR.
Example
The Code is as follows:
<Head>
<Script type = "text/javascript">
Function delRow ()
{
Document. getElementById ('mytable'). deleteRow (0)
}
</Script>
</Head>
<Body>
<Table id = "myTable" border = "1">
<Tr>
<Td> Row1 cell1
<Td> Row1 cell2
</Tr>
<Tr>
<Td> Row2 cell1
<Td> Row2 cell2
</Tr>
</Table>
<Br/>
<Input type = "button" onclick = "delRow ()"
Value = "Delete first row">
</Body>
</Html>
InsertCell ()
Definition and usage
The insertCell () method is used to insert an emptyElement.
Syntax
TablerowObject. insertCell (index)
Return Value
A TableCell object that is newly created and insertedElement.
Description
This method creates a newInsert it into the specified position in the row. The new cell is inserted before the table element at the specified position of the index. If index is equal to the number of cells in the row, the new cell is appended to the end of the row.
Note that this method can only be insertedData Table element. To add a header table element to a row, you must use the Document. createElement () method and Node. insertBefore () method (or related method) to create and insertElement.
Throw
If the index parameter is smaller than 0 or greater than or equal to the number of table elements in the row, this method throws a DOMException code INDEX_SIZE_ERR.
Example
The Code is as follows:
<Head>
<Script type = "text/javascript">
Function insCell ()
{
Var x = document. getElementById ('tr2'). insertCell (0)
X. innerHTML = "John"
}
</Script>
</Head>
<Body>
<Table border = "1">
<Tr id = "tr1">
<Th> Firstname
<Th> Lastname
</Tr>
<Tr id = "tr2">
<Td> Peter
<Td> Griffin
</Tr>
</Table>
<Br/>
<Input type = "button" onclick = "insCell ()" value = "Insert cell">
</Body>
</Html>
DeleteCell ()
Definition and usage
The deleteCell () method is used to delete cells (Element ).
Syntax
TablerowObject. deleteCell (index)
Description
The index parameter is the position of the table element to be deleted in the row.
This method deletes the table meta at the specified position in the table row.
Throw
If the index parameter is smaller than 0 or greater than or equal to the number of table elements in the row, this method throws a DOMException code INDEX_SIZE_ERR.
Example
The Code is as follows:
<Head>
<Script type = "text/javascript">
Function delCell ()
{
Document. getElementById ('tr2'). deleteCell (0)
}
</Script>
</Head>
<Body>
<Table border = "1">
<Tr id = "tr1">
<Th> Firstname
<Th> Lastname
</Tr>
<Tr id = "tr2">
<Td> Peter
<Td> Griffin
</Tr>
</Table>
<Br/>
<Input type = "button" onclick = "delCell ()" value = "Delete cell">
</Body>
</Html>
Project applications:
The Code is as follows: