Create a table dynamically using javascript, and create a table using javascript

Source: Internet
Author: User

Create a table dynamically using javascript, and create a table using javascript

Copy codeThe Code is as follows:
<Html>
<Head>
<Script>
Function createTable (rows, lines ){
This. rows = rows;
This. lines = lines;
Var Body = document. getElementById ('body ');
Var Table = document. createElement ('table'); // create a table label element
Table. setAttribute ('border', '1 ');
// Add other attributes to the table label
For (var I = 0; I <this. rows; I ++ ){
Var lRow = document. createElement ('tr ');
For (var j = 0; j <this. lines; j ++ ){
Var textNode = document. createTextNode (I + ',' + j );
Var lLine = document. createElement ('td ');
LLine. appendChild (textNode );
LRow. appendChild (lLine );
}
Table. appendChild (lRow );
}
Body. appendChild (Table );
}
</Script>
</Head>
<Body>
<Div id = "body"> </div>
</Body>
<Script type = "text/javascript">
CreateTable (10, 10 );
</Script>
</Html>

Method 2:

Copy codeThe Code is as follows:
<Script>
Function createTable (rows, lines ){
This. rows = rows;
This. lines = lines;
Var Body = document. getElementById ('body ');
Var Table = document. createElement ('table ');
Table. setAttribute ('border', 1 );
For (var I = 0; I <this. rows; I ++ ){
Var row = Table. insertRow (I );
For (var j = 0; j <this. lines; j ++ ){
Var cells = row. insertCell (j );
Cells. innerHTML = I + ',' + j
}
}
Body. appendChild (Table );

}
</Script>

Related Article

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.