JS dynamic creation of table IE does not display the fix

Source: Internet
Author: User

The following method creates a table and attaches it to the DOM tree, but it is not displayed in IE:

VaR table = Document. createelement ("table ");

VaR TR = Document. createelement ("TR ");

VaR TD = Document. createelement ("TD ");

VaR text = Document. createtextnode ("text ");

TD. appendchild (text );

Tr. appendchild (TD );

Table. appendchild (TR );

Document. Body. appendchild (table );

No error is reported. You can also see that the entire table is completely created and attached to the body, but it is not displayed. This is because TR is not allowed to be directly appended to the table in IE. Instead, you must first attach tr to the tbody and then attach the tbody to the table. So the correct one should be:

VaR table = Document. createelement ("table ");

VaR tbody = Document. createelement ("tbody ");

VaR TR = Document. createelement ("TR ");

VaR TD = Document. createelement ("TD ");

VaR text = Document. createtextnode ("text ");

TD. appendchild (text );

Tr. appendchild (TD );

Tbody. appendchild (TR)

Table. appendchild (tbody );

Document. Body. appendchild (table );

This problem does not exist in Firefox. Note that tbody elements are not mandatory when a table is constructed using HTML explicitly. Maybe IE will automatically add tbody during parsing. Otherwise, the explanation will fail?

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.