Home  >  Article  >  Web Front-end  >  How to set up a table in javascript

How to set up a table in javascript

PHPz
PHPzOriginal
2023-04-21 09:09:322327browse

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:

var table = document.createElement("table"); // 创建一个table元素
table.innerHTML = "<tr><td>第一行,第一列</td><td>第一行,第二列</td></tr><tr><td>第二行,第一列</td><td>第二行,第二列</td></tr>"; // 设置表格内容
document.body.appendChild(table); // 将表格添加到页面中

The other method is to use DOM to create a table, the code is as follows:

var table = document.createElement("table"); // 创建一个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); // 将表格添加到页面中

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:

var table = document.createElement("table"); // 创建一个table元素
table.innerHTML += "<tr><td>第一行,第一列</td><td>第一行,第二列</td></tr>"; // 添加一行
table.innerHTML += "<tr><td>第二行,第一列</td><td>第二行,第二列</td></tr>";
document.body.appendChild(table); // 将表格添加到页面中

The other method is to use DOM to add rows and cells, the code is as follows:

var table = document.createElement("table"); // 创建一个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); // 将表格添加到页面中

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:

var table = document.createElement("table"); // 创建一个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); // 将表格添加到页面中

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn