HTML DOM TableRow 对象
htmldomweb developmentfront end technology更新于 2025/11/29 11:56:17
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>
输出

点击"创建表格行"来创建表格行。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

