JavaScript is a widely used scripting language that can be used to make interactive web applications and is also suitable for setting up HTML tables. This article will introduce how to use JavaScript to set up HTML tables, including table creation, adding rows and cells, and style settings.
1. Create HTML tables
There are two ways to create tables in JavaScript. The first is to use the innerHTML attribute, the code is as follows:
1 2 3 | var table = document.createElement( "table" );
table.innerHTML = "<tr><td>第一行,第一列</td><td>第一行,第二列</td></tr><tr><td>第二行,第一列</td><td>第二行,第二列</td></tr>" ;
document.body.appendChild(table);
|
Copy after login
The other method is to use DOM to create a table, the code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var table = document.createElement( "table" );
var row1 = document.createElement( "tr" );
var cell1 = document.createElement( "td" );
var cell2 = document.createElement( "td" );
cell1.innerHTML = "第一行,第一列" ;
cell2.innerHTML = "第一行,第二列" ;
row1.appendChild(cell1);
row1.appendChild(cell2);
var row2 = document.createElement( "tr" );
var cell3 = document.createElement( "td" );
var cell4 = document.createElement( "td" );
cell3.innerHTML = "第二行,第一列" ;
cell4.innerHTML = "第二行,第二列" ;
row2.appendChild(cell3);
row2.appendChild(cell4);
table.appendChild(row1);
table.appendChild(row2);
document.body.appendChild(table);
|
Copy after login
2. Add rows and cells
There are also two methods for adding table rows and cells in JavaScript. One is to use the innerHTML attribute, the code is as follows:
1 2 3 4 | var table = document.createElement( "table" );
table.innerHTML += "<tr><td>第一行,第一列</td><td>第一行,第二列</td></tr>" ;
table.innerHTML += "<tr><td>第二行,第一列</td><td>第二行,第二列</td></tr>" ;
document.body.appendChild(table);
|
Copy after login
The other method is to use DOM to add rows and cells, the code is as follows:
1 2 3 4 5 6 7 8 9 10 | var table = document.createElement( "table" );
var row = document.createElement( "tr" );
var cell1 = document.createElement( "td" );
var cell2 = document.createElement( "td" );
cell1.innerHTML = "新行,第一列" ;
cell2.innerHTML = "新行,第二列" ;
row.appendChild(cell1);
row.appendChild(cell2);
table.appendChild(row);
document.body.appendChild(table);
|
Copy after login
3. Style settings
Styling the table is also easy using JavaScript. You can use the style attribute to set the style of rows and cells. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var table = document.createElement( "table" );
var row = document.createElement( "tr" );
var cell1 = document.createElement( "td" );
var cell2 = document.createElement( "td" );
cell1.innerHTML = "新行,第一列" ;
cell2.innerHTML = "新行,第二列" ;
cell1.style.border = "1px solid black" ;
cell2.style.border = "1px solid black" ;
row.appendChild(cell1);
row.appendChild(cell2);
row.style.backgroundColor = "cyan" ;
table.appendChild(row);
table.style.borderCollapse = "collapse" ;
document.body.appendChild(table);
|
Copy after login
Summary:
This article introduces how to use JavaScript to set HTML tables, including table creation, adding rows and cells format, and style settings. Setting the table through JavaScript can make the table style more flexible and beautiful. Developers can modify and expand according to their own needs to achieve more complex and efficient table settings.
The above is the detailed content of How to set up a table in javascript. For more information, please follow other related articles on the PHP Chinese website!