这篇文章主要介绍了JS实现向表格行添加新单元格的方法,涉及javascript针对表格进行动态操作的技巧,需要的朋友可以参考下。
下面的JS代码可以想表格中指定id的行插入新的单元格:
<!DOCTYPE html>
<html>
<head>
<script>
function insCell()
{
var x=document.getElementById('tr1').insertCell(0); x.innerHTML="The famous";
}
</script>
</head>
<body>
<table border="1">
<tr id="tr1">
<td>Peter</td>
<td>Griffin</td>
</tr>
</table>
<br>
<input type="button" onclick="insCell()" value="Insert cell">
</body>
</html>