HTML DOM TableRow 物件


HTML DOM TableRow 物件表示 HTML 文件的 <tr> 元素。

建立 TableRow 物件

語法

以下是語法 -

document.createElement(“TR”);

TableRow 物件的屬性

屬性說明
rowIndex它返回表的行集合中某行的位置。
sectionRowIndex它返回表頭、表主體或表尾的行集合中某行的位置。

TableRow 物件的方法

方法說明
deleteCell()它從當前表格行中移除單元格。
insertCell()它在當前表格行中新增單元格。

讓我們看一個 TableRow 物件示例

示例

 即時演示

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      background: lightblue;
      height: 100vh;
      text-align: center;
   }
   table {
      margin: 2rem auto;
      width: 400px;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
</style>
<body>
<h1>DOM TableRow Object Demo</h1>
<table border="2">
<thead>
<tr>
<th>Name</th>
<th>Language</th>
</tr>
<thead>
<tbody class="table-body">
<tr>
<td>John</td>
<td>English</td>
</tr>
<tr>
<td>Elon</td>
<td>Germany</td>
</tr>
</tbody>
</table>
<button onclick="get()" class="btn">Create TableRow</button>
<script>
   function get() {
      var tr = document.createElement("TR");
      tr.innerHTML = "<td>Mario</td><td>French</td>"
      document.querySelector(".table-body").appendChild(tr);
   }
</script>
</body>
</html>

輸出

單擊“建立 TableRow”以建立表格行。

更新時間:20-Sep-2019

65 次瀏覽

開啟您的 職業生涯

透過完成課程,獲得認證

開始
廣告