How JavaScript dynamically loads tables and dynamically adds table rows _javascript tips

Source: Internet
Author: User
One, dynamic loading table

1. Set the ID first in HTML for the add position of the table

Which is to write a DIV tag inside the HTML body tag to indicate that the table is to be added to the inside of this div. As follows

<div id= "TDL" ><div>

2. Write a statement to add a table in JavaScript

In the current HTML file, it is written inside the <script> tag, as
Copy Code code as follows:

<script type= "Text/javascript" >

document.getElementById ("TBL"). Innerhtml= "<table><tr><td></td></tr></table > "//The form added here can be created according to your needs

</script>

If through the introduction of JS file, in the JS file (assuming that is test.js) directly write the following statement
Copy Code code as follows:

document.getElementById ("TBL"). Innerhtml= "<table><tr><td></td></tr></table > "

And then introduce your own HTML file.
Copy Code code as follows:

<script type= "Text/javascript" src= "Test.js" ></script>

second, dynamically add table rows

1. First set the ID for the add position of the table row in HTML, this location must be <tbody> internal (not particularly accurate, but according to my test to get this conclusion, there are other methods please leave a message, thank you), as follows
Copy Code code as follows:

<table>
<thead></thead>

<tfoot><tfoot>//tfoot and Thead are compatible with tbody, but when I write, it is useless.

<tbody id= "Rows" ></tbody>

</table>
[\s\s]*\n
2. In JavaScript content, first create rows and cells, and then add rows to <.tbody>, as follows
[Code]
Row=document.createelement ("tr"); Create a row

Td1=document.createelement ("tr"); Create cells

Td1.appendchild (document.createTextNode ("content")); Add content to a cell

Row.appendchild (TD1); Add a cell to a row

document.getElementById ("Rows"). Append (row); Add Rows to <tbody>


Third, my small discovery (perhaps others already knew 、、、)

1. I did a test myself, writing in HTML <table id= "TDL" ></table>,javascript write document.getElementById ("TDL"). Innerhtml= " <tr><td></td></tr>, which is written and tested, and the rows of table in HTML are not added.

2. After the above test, I changed the HTML to write <table><tr><td id= "T ' ><td><tr></table> Write document.getElementById ("T") in JavaScript. innerhtml= "<p>content</p>", the test can add content.

3. Thinking: From the above two tests seem to be able to draw some conclusions, how to sum up has not thought well, which label can directly through the innerHTML directly add it?

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.