Dynamic Creation of table tables using JavaScript

Source: Internet
Author: User

Dynamic Creation of table tables using JavaScript
Introduction:

The original table was written by ourselves. We did not write it in the body label, so it was generated through the code in JavaScript:

Method 1:

The original method creates one-to-one elements.
Var a1 = document. createElement (table );
Var a2 = document. createElement (tbody );
Var a3 = document. createElement (tr );
Var a4 = document. createElement (td );
// Start appendchild () to append each element.
A3.appendChild (a4 );
A2.appendChild (a3 );
A1.appendChild (a2 );

 

Method 2:

Use the functions contained in the table object: Insert rows and insert Columns

Var tabNode = document. createElement (table );
Var trNode = tabNode. insertRow ();
Var tdNode = trNode. insertCell;
TabNode. innerHTML = This is created using the functions in the table object.

Note: using the original method, a tbody object must be added when createElement is created.

// Obtain

Sibling node of the tag
// Var node3 = tabnode. previussibling; // The previous node obtains the reference of the previous sibling object of this object.
// Alert (previous -- node3: + node3); // # text
// If
There is a carriage return character. Later versions of IE and Firefox will be recognized as "blank text" # text,
// In earlier versions, IE will directly go beyond ----- not only
The same is true for other nodes.
// Table,
There is actually a tag-Table body hidden between the tag and the tag.

 

 

Dynamic Creation and deletion:

Create a table by entering the following values:

 

Function createTable () {tableNode = document. createElement (table); // obtain the tableNode object. setAttribute (id, table) var row = parseInt (document. getElementsByName (row1) [0]. value); // obtain the row number // alert (row); if (row <= 0 | isNaN (row) {alert (the entered row number is incorrect, and the table cannot be created, enter again :); return;} var cols = parseInt (document. getElementsByName (cols1) [0]. value); if (isNaN (cols) | cols <= 0) {alert (the entered column number is incorrect. You cannot create a table. Please enter it again :); return ;} // The preceding figure shows how to create for (var x = 0; x
 
  

 

Delete row:
Function delRow () {// to delete a row, you must obtain the table object to delete the row. Therefore, you must set the table Object id when creating the table object to facilitate the var tab = document operation. getElementById (table); // obtain the table object if (tab = null) {alert (the deleted table does not exist !) Return;} var rows = parseInt (document. getElementsByName (delrow1) [0]. value); // obtain the object to be deleted if (isNaN (rows) {alert (the input row is incorrect. Enter the row to be deleted ...); Return;} if (rows >=1 & rows <= tab. rows. length) {tab. deleteRow (rows-1);} else {alert (the deleted row does not exist !!); Return ;}}
Delete column:
// It is troublesome to delete a column. You need to delete the cells in a row through the rows. // the length of a row is the number of columns. // tab. rows [x]. deleteCell (cols-1) function delCols () {// obtain the table object var tab = document. getElementById (table); if (tab = null) {alert (the deleted table does not exist !!); Return;} // obtain the content var cols = parseInt (document. getElementsByName (delcols1) [0]. value); // check for reliability if (isNaN (cols) {alert (incorrect input. Enter the column to be output ..); Return;} if (! (Cols> = 1 & cols
   
    
Complete code:
    

    
    <Script type = text/javascript> var tableNode; function createTable () {tableNode = document. createElement (table); // obtain the tableNode object. setAttribute (id, table) var row = parseInt (document. getElementsByName (row1) [0]. value); // obtain the row number // alert (row); if (row <= 0 | isNaN (row) {alert (the entered row number is incorrect, and the table cannot be created, enter again :); return;} var cols = parseInt (document. getElementsByName (cols1) [0]. value); if (isNaN (cols) | cols <= 0) {alert (the entered column number is incorrect. You cannot create a table. Please enter it again :); return ;} // The preceding figure shows how to create for (var x = 0; x
    
     
= 1 & rows <= tab. rows. length) {tab. deleteRow (rows-1);} else {alert (the deleted row does not exist !!); Return;} // it is more difficult to delete a column. The length of cells in a row is the number of columns. // tab. rows [x]. deleteCell (cols-1) function delCols () {// obtain the table object var tab = document. getElementById (table); if (tab = null) {alert (the deleted table does not exist !!); Return;} // obtain the content var cols = parseInt (document. getElementsByName (delcols1) [0]. value); // check for reliability if (isNaN (cols) {alert (incorrect input. Enter the column to be output ..); Return;} if (! (Cols> = 1 & cols
     Column:
      
     
     


 
Demo:

 

 

 

 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.