天天看点

tr insertCell() 方法

tr 对象

定义和用法

insertCell() 方法用于在 HTML 表的一行的指定位置插入一个空的 <td> 元素。

语法

trObject.insertCell(index)

描述
index 该方法将创建一个新的 <td> 元素,把它插入行中指定的位置。新单元格将被插入当前位于 index 指定位置的表元之前。如果 index 等于行中的单元格数,则新单元格被附加在行的末尾。

浏览器支持

tr insertCell() 方法
tr insertCell() 方法
tr insertCell() 方法
tr insertCell() 方法
tr insertCell() 方法

所有主流浏览器都支持 insertCell() 方法

实例

下面的例子在表格行中插入了一个单元格:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>菜鸟教程(runoob.com)</title>

<script>

function displayResult(){

    var firstRow=document.getElementById("myTable").rows[0];

    var x=firstRow.insertCell(-1);

    x.innerHTML="New cell"

}

</script>

</head>

<body>

<table id="myTable" border="1">

    <tr>

        <td>First cell</td>

        <td>Second cell</td>

        <td>Third cell</td>

    </tr>

</table>

<br>

<button type="button" onclick="displayResult()">插入单元格</button>

</body>

</html>

tr insertCell() 方法

更多实例

向表格添加新行 - 然后添加单元格和内容

tr 对象