---
title: "js 动态为talbe 添加行"
author: "txf2004"
date: "2009-08-10T17:47:00Z"
tags: []
slug: "js-talbe"
draft: false
lastmod: "2009-09-18T18:22:44Z"
id: 471139
url: "https://txf2004.iteye.com/blog/471139"
---

[![http://www.corange.cn//uploadfiles/1106_17915.jpg](http://www.corange.cn//uploadfiles/1106_17915.jpg)](http://www.corange.cn//uploadfiles/1106_17915.jpg)

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;  
&lt;style type="text/css"&gt;  
body{background:#FFFFFF;}  
&lt;/style&gt;  
&lt;script type="text/javascript"&gt;  
function add(){  
var oTr = document.getElementById("addtr").rows\[1];  
var newTr = oTr.cloneNode(true);  
document.getElementById("addtr").getElementsByTagName("tbody")\[0].appendChild(newTr);  
newTr.cells\[0].firstChild.value = newTr.rowIndex;  
document.getElementById("b1").disabled = newTr.rowIndex ==5 ;  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;input type="button" id="b1" value="添加一行" onclick="add()" /&gt;  
&lt;table width="700" border="0" cellspacing="0" cellpadding="0" id="addtr"&gt;  
&lt;tr&gt;  
&lt;td height="30" align="center" bgcolor="#CCCCCC"&gt;ID&lt;/td&gt;  
&lt;td align="center" bgcolor="#CCCCCC"&gt;Username&lt;/td&gt;  
&lt;td align="center" bgcolor="#CCCCCC"&gt;Usertype&lt;/td&gt;  
&lt;td align="center" bgcolor="#CCCCCC"&gt;Other&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td height="30" align="center"&gt;&lt;input type="text" size="2" value="1" /&gt;&lt;/td&gt;  
&lt;td align="center"&gt;&lt;input type="text" name="username" /&gt;&lt;/td&gt;  
&lt;td align="center"&gt;  
&lt;select name="type"&gt;  
&lt;option value="1"&gt;Administrator&lt;/option&gt;  
&lt;option value="2"&gt;Guest&lt;/option&gt;  
&lt;/select&gt;  
&lt;/td&gt;  
&lt;td align="center"&gt;&lt;input type="text" name="username2" /&gt;&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;  
&lt;/body&gt;  
&lt;/html&gt;