向表格中插入一行

<wbr>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a rel="nofollow" href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"&gt;<br>&lt;html xmlns="<a rel="nofollow" href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>"&gt;<br>&lt;head&gt;<br>&lt;meta http-equiv="Content-Type" content="text/html; charset=gbk" /&gt;<br>&lt;title&gt;无标题文档&lt;/title&gt;<br>&lt;script type="text/javascript"&gt;<br>//每一行的索引<br>var index = 3;<br>function btnAddRow(){<br>//创建新行的id<br>var rId = "tr" + index;<br>//随机数<br>var ranPrice = "¥" + Math.round((Math.random() * 100));<br>//table对象<br>var table1 = document.getElementById("table1");<br>//行数<br>var rowCount = table1.rows.length;<br>//添加到最后一行<br>var newRow = table1.insertRow(rowCount);<br>//添加ID<br>newRow.id = rId;<br>//给第一列添加<br>var newCell1 = newRow.insertCell(0);<br>//内容<br>newCell1.innerHTML = "西游记"+index;<br>//给第二列添加<br>var newCell2 = newRow.insertCell(1);<br>//内容<br>newCell2.innerHTML = ranPrice;<br>//给第三列添加<br>var newCell3 = newRow.insertCell(2);<br>//内容<br>newCell3.innerHTML = "&lt;input type='button' name='button' id='button' value='删除' /&gt;";<br>//alert(newCell3.innerHTML);<br>//变量++<br>index ++;<br>}</wbr>

</script>
</head>

<body>
<table id="table1" width="500" border="1" cellspacing="1" cellpadding="1">
<tr id="tr0">
<td>书名</td>
<td>价格</td>
<td>操作</td>
</tr>
<tr id="tr1">
<td>三国演义</td>
<td>¥99</td>
<td>
<input type="button" name="button" id="button" value="删除" />&nbsp;</td>
</tr>
<tr id="tr2">
<td>水浒传</td>
<td>¥100</td>
<td><input type="button" name="button2" id="button2" value="删除" /></td>
</tr>
</table>
<p>
<input type="button" name="button3" id="button3" value="新增一行" onclick="btnAddRow()" />
</p>
</body>
</html>

你可能感兴趣的:(表格)