How does javascript edit tables?

12-14-2023

Automatic code writing robot, free of charge.

Today, Xiaobian will share with you the relevant knowledge points of how javascript edits tables. The content is detailed and the logic is clear. I believe most people still know this knowledge too well, so I will share this article for your reference. I hope you will gain something after reading this article. Let's take a look at it together.

First, the basic knowledge of forms

In HTML, tables are implemented with tags such as table, tr, th and td, where table represents the whole table, tr represents rows in the table, th represents header cells in the table, and td represents data cells in the table. The following is a simple table:

   (full) name age gender           Tom     20 man           Mary     25 woman   

So you can show a table with three columns of data on the page. Next, we will take this table as an example to show how to edit it through JavaScript.

Second, get the table object

In JavaScript, we can get table objects through the getElementById method or the getElementsByTagName method of the document object, for example:

var table = document.getElementById("tableId"); //Get the table object by id var td = document.getElementsByTagName("td"); //Get all table cell objects by tag name.

Using getElementById method to obtain table objects needs to add id attribute to the corresponding table in HTML, for example:

  

Third, get the rows and cells in the table

Getting rows and cells in a table is one of the most basic operations for operating a table. We can get all the rows of the table through the rows property of the table object, and get all the cells in the row through the cells property of the row object. For example:

//Get header line object var thead = table.rows[0]; //Get the first line of objects var tr = table.rows[1]; //Get the cell object in the first row and the first column. var td = table.rows[1].cells[0]; //Get the cell object in the second row and the third column. var td2 = table.rows[2].cells[2];

Fourth, add rows and cells

It is often necessary to dynamically add rows and cells in a table. We can add rows through the insertRow method, which returns a row object, and we can add cells through the insertCell method of this object. For example:

//Insert a row in the second position of the table. var newRow = table.insertRow(1); //Insert a cell at the first position of the row. var newCell = newRow.insertCell(0); newCell.innerHTML = 'Jerry'; //Insert a cell in the second position of the row. newCell = newRow.insertCell(1); newCell.innerHTML = 18; //Insert a cell in the third position of the row. newCell = newRow.insertCell(2); NewCell.innerHTML =' male';

This inserts a row in the second position of the table and three cells in the row.

V. Delete rows and cells

Deleting rows and cells in a table is also a common operation. We can delete rows through the remove method of the row object or delete cells through the remove method of the cell object. For example:

//Get the third line object var row = table.rows[2]; //Delete this line table.deleteRow(row.rowIndex); //Get the second cell object in the first row var cell = table.rows[0].cells[1]; //Delete the cell cell.parentNode.removeChild(cell);

Sixth, merge cells

Sometimes we need to merge adjacent cells in a table to lay out a page or display data. We can use the rowSpan and colSpan properties to merge cells. For example:

//Merge the cells in the first row, the first column and the second column. var td1 = table.rows[0].cells[0]; td1.rowSpan = 2; //Delete the cell in the first column of the second row. table.rows[1].deleteCell(0);

This will merge the cells in the first row and the second column, and delete the cells in the second row and the first column.

Seven, modify the cell content

Besides adding, deleting and merging cells, modifying cell contents is also one of the common operations. We can modify the text content inside the cell through the innerHTML property or the innerText property of the cell object. For example:

//Get the cell object in the second row and the third column. var td = table.rows[2].cells[2]; //Modify the text content in the cell. Td.innerHTML =' female';

At this point, the cell text content in the second row and the third column will become female.

That's all about how javascript edits tables. Thank you for reading! I believe that everyone has gained a lot after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the Yisu cloud industry information channel.

Ask AI for details

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us