Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> js dynamic operation table </title>
<Script language = "javascript">
Function init (){
_ Table = document. getElementById ("table ");
_ Table. border = "1px ";
_ Table. width = "800px ";
For (var I = 1; I <6; I ++ ){
Var row = document. createElement ("tr ");
Row. id = I;
For (var j = 1; j <6; j ++ ){
Var cell = document. createElement ("td ");
Cell. id = I + "/" + j;
Cell. appendChild (document. createTextNode ("column" + cell. id + "));
Row. appendChild (cell );
}
Document. getElementById ("newbody"). appendChild (row );
}
}
Function rebulid (){
Var beginRow = document. getElementById ("beginRow"). value;/* start line */
Var endRow = document. getElementById ("endRow"). value;/* end line */
Var beginCol = document. getElementById ("beginCol"). value;/* Start column */
Var endCol = document. getElementById ("endCol"). value;/* end column */
Var tempCol = beginRow + "/" + beginCol;/* locate the column to change the attribute */
Alert (tempCol );
Var td = document. getElementById (tempCol );
For (var x = beginRow; x <= endRow; x ++ ){
For (var I = beginCol; I <= endCol; I ++ ){
If (x = beginRow ){
Document. getElementById ("table"). rows [x]. deleteCell (I + 1 );
}
Else {
Document. getElementById ("table"). rows [x]. deleteCell (I );
}
}
}
Td. rowSpan = (endRow-beginRow) + 1;
}
/* Add a row and use the appendChild Method */
Function addRow (){
Var length = document. getElementById ("table"). rows. length;
/* Document. getElementById ("newbody"). insertRow (length );
Document. getElementById (length + 1). setAttribute ("id", length + 2 );*/
Var tr = document. createElement ("tr ");
Tr. id = length + 1;
Var td = document. createElement ("td ");
For (I = 1; I <4; I ++ ){
Td. id = tr. id + "/" + I;
Td. appendChild (document. createTextNode ("th" + td. id + "column "));
Tr. appendChild (td );
}
Document. getElementById ("newbody"). appendChild (tr );
}
Function addRow_withInsert (){
Var row = document. getElementById ("table"). insertRow (document. getElementById ("table"). rows. length );
Var rowCount = document. getElementById ("table"). rows. length;
Var countCell = document. getElementById ("table"). rows. item (0). cells. length;
For (var I = 0; I <countCell; I ++ ){
Var cell = row. insertCell (I );
Cell. innerHTML = "new" + (rowCount) + "/" + (I + 1) + "column ";
Cell. id = (rowCount) + "/" + (I + 1 );
}
}
/* Delete a row using deleteRow (row Index )*/
Function removeRow (){
Document. getElementById ("newbody"). deleteRow (document. getElementById (document. getElementById ("table"). rows. length). rowIndex );
}
/* Add a column using the insertCell (column position) Method */
Function addCell (){
/* Document. getElementById ("table"). rows. item (0). cells. length
Used to obtain the number of columns in a table
*/
For (var I = 0; I <document. getElementById ("table"). rows. length; I ++ ){
Var cell = document. getElementById ("table"). rows [I]. insertCell (2 );
Cell. innerHTML = "th" + (I + 1) + "/" + 3 + "column ";
}
}
/* Delete a column by using deleteCell (column position */
Function removeCell (){
For (var I = 0; I <document. getElementById ("table"). rows. length; I ++ ){
Document. getElementById ("table"). rows [I]. deleteCell (0 );
}
}
</Script>
</Head>
<Body onLoad = "init ();">
<Table id = "table" align = "center">
<Tbody id = "newbody"> </tbody>
</Table>
<Div>
<Table width = "800px" border = "1px" align = "center">
<Tr> <td align = "center"> <input type = "button" id = "addRow" name = "addRow" onClick = "addRow (); "value =" add row "/> </td> <td align =" center "> <input type =" button "id =" delRow "name =" delRow "onClick = "removeRow (); "value =" Delete row "/> </td> </tr>
<Tr> <td align = "center"> <input type = "button" id = "delCell" name = "delCell" onClick = "removeCell (); "value =" delete column "/> </td> <td align =" center "> <input type =" button "id =" addCell "name =" addCell "onClick = ""addCell (); "value =" add column "/> </td> </tr>
<Tr> <td align = "center" colspan = "2"> <input type = "button" id = "addRows" name = "addRows" onClick = "addRow_withInsert (); "value =" add row "/> </td> </tr>
</Table>
</Div>
<Div>
<Table width = "800px" border = "1px" align = "center">
<Tr> <td> from the <input type = "text" id = "beginRow" name = "beginRow" value = ""/> row to <input type = "text" name = "endRow" id = "endRow" value = ""/> row </td> <td rowspan = "2" id = "test"> <input type = "button" name = "hebing" id = "hebing" value = "merge" onClick = "rebulid (); "/> </td> </tr>
<Tr> <td> column <input type = "text" name = "beginCol" id = "beginCol" value = ""/> to <input type = "text" name = "endCol" id = "endCol" value = ""/> column </td> </tr>
</Table>
</Div>
</Body>
</Html>