Considerations for dynamic table creation using JavaScript

Source: Internet
Author: User
/** Use Dom to dynamically create elements. Note that the tr element cannot be directly attached to the appendchild method.
Add to the table element. The TR element must be appended to the tbody element with the appendchild method, and then
The appendchild method attaches the tbody to the table element. Otherwise, no error is reported on the page,
Is not displayed
*/
Function createtab (){
VaR etab = Document. createelement ("table ");
Etab. Border = 1; // The border is 1
VaR etbody = Document. createelement ("tbody ");
VaR etr1 = Document. createelement ("TR ");
VaR etr2 = Document. createelement ("TR ");

VaR etd1 = Document. createelement ("TD ");
VaR etd2 = etd1.clonenode (); // You can also use the clone method.
VaR etd3 = Document. createelement ("TD ");
VaR etd4 = Document. createelement ("TD ");
VaR etd5 = Document. createelement ("TD ");
VaR etd6 = Document. createelement ("TD ");

VaR txtnode1 = Document. createtextnode ("1 ");
VaR txtnode2 = txtnode1.clonenode (); // You can also use the clone method.
Txtnode2.nodevalue = "dfdf"; // use nodevalue to assign a value again.
VaR txtnode3 = Document. createtextnode ("3 ");
VaR txtnode4 = Document. createtextnode ("4 ");
VaR txtnode5 = Document. createtextnode ("5 ");
VaR txtnode6 = Document. createtextnode ("6 ");

Etd1.appendchild (txtnode1 );
Etd2.appendchild (txtnode2 );
Etd3.appendchild (txtnode3 );
Etd4.appendchild (txtnode4 );
Etd5.appendchild (txtnode5 );
Etd6.appendchild (txtnode6 );

Etr1.appendchild (etd1 );
Etr1.appendchild (etd2 );
Etr1.appendchild (etd3 );

Etr2.appendchild (etd4 );
Etr2.appendchild (etd5 );
Etr2.appendchild (etd6 );


Etbody. appendchild (etr1 );
Etbody. appendchild (etr2 );

Etab. appendchild (etbody );
Document. Body. appendchild (etab );


}

/* Use the innerhtml attribute of the page element to dynamically create the page element, which is used when adding text.
*/
Function createbyinnerhtml (){
VaR OBJ = Document. getelementbyid ("TT ");
OBJ. innerhtml = "<Table border = ''1''> <tr> <TD> dfdf </TD> </tr> </table> ";
}

</SCRIPT>

<Div id = "TT"> </div>

 

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.