HTML DOM 表格物件
HTML DOM 表格物件表示 HTML 文件中的 <table> 元素。
讓我們看看如何建立表格物件。
語法
以下是語法:
document.createElement(“TABLE”);
屬性
以下是表格物件的屬性:
屬性 | 說明 |
---|---|
caption | 它返回 HTML 文件中表格的 <caption> 元素。 |
tFoot | 它返回 HTML 文件中表格的 <tfoot> 元素。 |
tHead | 它返回 HTML 文件中表格的 <thead> 元素。 |
方法
以下是表格物件的方法:
方法 | 說明 |
---|---|
createCaption() | 它生成一個空的 <caption> 元素並將其新增到表格中。 |
createTFoot() | 它生成一個空的 <tfoot> 元素並將其新增到表格中。 |
createTHead() | 它生成一個空的 <thead> 元素並將其新增到表格中。 |
deleteCaption() | 它刪除表格中的第一個 <caption> 元素。 |
deleteRow() | 它刪除表格中的 <tr> 元素。 |
deleteThead() | 它刪除表格中的 <thead> 元素。 |
deleteTFoot() | 它刪除表格中的 <tfoot> 元素。 |
insertRow() | 它生成一個空的 <tr> 元素並將其新增到表格中。 |
示例
讓我們看看錶格物件的示例:
<!DOCTYPE html> <html> <head> <style> body { text-align: center; background-color: #fff; color: #0197F6; } h1 { color: #23CE6B; } .btn { background-color: #fff; border: 1.5px dashed #0197F6; height: 2rem; border-radius: 2px; width: 60%; margin: 2rem auto; display: block; color: #0197F6; outline: none; cursor: pointer; } </style> </head> <body> <h1>DOM table Object Demo</h1> <button onclick="createTable()" class="btn">Create a table object</button> <script> function createTable() { var table = document.createElement("TABLE"); table.innerHTML = `<tr> <td>Data 1</td> <td>Data 2</td> </tr> <tr> <td>Data 3</td> <td>Data 4</td> </tr>`; table.setAttribute('border', "2"); table.style.margin = "2rem auto"; document.body.appendChild(table); } </script> </body> </html>
輸出
這將產生以下輸出:
點選“建立表格物件”按鈕以建立表格物件。
廣告