DOM operation table example (DOM creates table)

DOM operation table example (DOM creates table)

1. Create a table using HTML tags:


Copy code
The code is as follows:

<tableborder="1"width="300">
<caption>Staff List</caption>
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>Zhang San</td>
<td>Male</td>
<td>20</td>
</tr>
<tr>
<td>Li Si</td>
<td>Female</td>
<td>22</td>
</tr>
</tbody>
<tfoot>
<tr>
<tdcolspan="3">Total: N</td>
</tr>
</tfoot>
</table>

There can be only one thead, tfoot, and caption tag in a table. There can be N tbody, tr, td, and th tags in a table.

2. Create a table using DOM

The <table> tag has the most complex structure in HTML. We can create it through DOM or manipulate it through HTMLDOM. (HTMLDOM provides a more convenient and quick way to operate HTML)


Copy code
The code is as follows:

<script>
window.onload = function(){
vartable = document.createElement("table");
//Add attributes to the table
table.width=300;//You can also use this method: table.setAttribute('width',300)
table.border=1;</p> <p>//Create a table title
varcaption = document.createElement("caption");
table.appendChild(caption);</p> <p>//Add content to the table title
//caption.innerHTML="Staff table"; //Non-W3c standard method
varcaptionText=document.createTextNode("Personnel table");
caption.appendChild(captionText);</p> <p>
//Create the first row of the table, which is a header row
varthead=document.createElement("thead");
table.appendChild(thead);</p> <p>vartr=document.createElement("tr");
thead.appendChild(tr);</p> <p>//column
varth1=document.createElement("th");
tr.appendChild(th1);
th1.innerHTML="data";
varth2=document.createElement("th");
tr.appendChild(th2);
th2.innerHTML="data";</p> <p>document.body.appendChild(table);
};
</script>

3. Use DOM to get table data (using DOM to operate tables will be annoying)

Copy code
The code is as follows:

window.onload = function(){
vartable=document.getElementsByTagName("table")[0];
alert(table.children[0].innerHTML);
};

4. Use HTMLDOM to obtain table data (convenient, simple and clear).

Because the table is relatively complex and has many levels, it would be very uncomfortable to use the DOM learned before just to get a certain element, so using HTMLDOM will be much clearer.


Copy code
The code is as follows:

window.onload = function(){
//Use HTMLDOM to get table elements
vartable = document.getElementsByTagName('table')[0]; //Get table reference
//According to HTMLDOM to get the <caption> of the table
alert(table.caption.innerHTML); //Get the content of caption
//table.caption.innerHTML="Student Table"; //You can also set the value
};


Copy code
The code is as follows:

window.onload = function(){
//Use HTMLDOM to get table elements
vartable = document.getElementsByTagName('table')[0]; //Get table reference
//According to HTMLDOM to get the table header and footer <thead>, <tfoot>
alert(table.tHead); //Get the table header
alert(table.tFoot); //Get the footer of the table</p> <p> //According to HTMLDOM to get the table body<tbody>
alert(table.tBodies); //Get the collection of table bodies
};

<thead> and <tfoot> are unique in a table and there can only be one. However, <tbody> is not unique and can have multiple elements, which results in the final returned <thead> and <tfoot> being element references, while <tbody> returns an element collection.


Copy code
The code is as follows:

window.onload = function(){
//Use HTMLDOM to get table elements
vartable = document.getElementsByTagName('table')[0]; //Get table reference
//According to HTMLDOM to get the number of rows in the table
alert(table.rows.length); //Get the number of rows in the table body according to HTMLDOM
alert(table.tBodies[0].rows.length); //Get the number of rows in the body
};


Copy code
The code is as follows:

window.onload = function(){
//Use HTMLDOM to get table elements
vartable = document.getElementsByTagName('table')[0]; //Get the table reference</p> <p> //According to HTMLDOM to get the number of cells in the first row of the table body (tr)
alert(table.tBodies[0].rows[0].cells.length); //Get the number of cells in the first row
};


Copy code
The code is as follows:

window.onload = function(){
//Use HTMLDOM to get table elements
vartable = document.getElementsByTagName('table')[0]; //Get the table reference</p> <p> //According to HTMLDOM, get the content of the first cell in the first row of the table body (td)
alert(table.tBodies[0].rows[0].cells[0].innerHTML); //Get the contents of the first cell in the first row
};


Copy code
The code is as follows:

<script>
window.onload = function(){
//Use HTMLDOM to get table elements
vartable = document.getElementsByTagName('table')[0]; //Get the table reference</p> <p> //According to HTMLDOM to delete the title, header, footer, row, cell
//table.deleteCaption(); //Delete title
//table.deleteTHead(); //delete <thead>
//table.tBodies[0].deleteRow(0);//Delete a row of <tr>
//table.tBodies[0].rows[0].deleteCell(0); //Delete a cell of <td>
//table.tBodies[0].rows[0].deleteCell(1); //Delete the contents of a cell, which is equivalent to deleting a cell, and the following will hopefully be filled in
};
</script>

5. HTMLDOM creates a table


Copy code
The code is as follows:

window.onload = function(){
//Create a table according to HTMLDOM
vartable = document.createElement('table');
table.border=1;
table.width=300;</p> <p>table.createCaption().innerHTML='Staff table';</p> <p>//table.createTHead();
//table.tHead.insertRow(0);
varthead=table.createTHead(); //This method returns a reference
vartr=thead.insertRow(0);//This method returns a reference</p> <p>vartd=tr.insertCell(0);
//td.appendChild(document.createTextNode('data'));//One way to add data, you can also use the following method
td.innerHTML="data";
vartd2=tr.insertCell(1);
//td2.appendChild(document.createTextNode('data 2'));
td2.innerHTML="data 2";</p> <p>document.body.appendChild(table);
};When creating a table, there is no specific method for <table>, <tbody>, and <th>. You need to use document to create them. You can also simulate existing methods and write specific functions, such as insertTH().

<<:  Solve the hierarchy problem of child element z-index and parent element sibling nodes in CSS

>>:  Detailed explanation of MySQL DEFINER usage

Recommend

ElementUI implements sample code for drop-down options and multiple-select boxes

Table of contents Drop-down multiple-select box U...

Vue3 (Part 2) Integrating Ant Design Vue

Table of contents 1. Integrate Ant Design Vue 2. ...

How to modify the sources.list of Ubuntu 18.04 to Alibaba or Tsinghua mirror

1. Backup source list The default source of Ubunt...

Detailed Analysis of Event Bubbling Mechanism in JavaScript

What is bubbling? There are three stages in DOM e...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

How to monitor mysql using zabbix

Zabbix deployment documentation After zabbix is ​...

Detailed explanation of docker network bidirectional connection

View Docker Network docker network ls [root@maste...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

MySQL uses variables to implement various sorting

Core code -- Below I will demonstrate the impleme...

Several ways to change MySQL password

Preface: In the daily use of the database, it is ...

Example code for CSS columns to achieve two-end alignment layout

1. Going around in circles After going around in ...

WeChat applet to determine whether the mobile phone number is legal example code

Table of contents Scenario Effect Code Summarize ...

Detailed explanation of bash command usage

On Linux, bash is adopted as the standard, which ...

Complete steps to install Anaconda3 in Ubuntu environment

Table of contents Introduction to Anaconda 1. Dow...