js控制新增,删除table的tr,td
2014-06-23 15:04
357 查看
<html>
<head>
<title>js增加td,tr</title>
<script language="javascript">
//add a new row to the table
function addRow()
{
//add a row to the rows collection and get a reference to the newly added row
var newRow = document.all("tblGrid").insertRow();
//add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes
var oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t1'>";
oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t2'>";
oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t3'> <input type='button' value='Delete'
onclick='removeRow(this);'/>";
}
//deletes the specified row from the table
function removeRow(src)
{
/* src refers to the input button that was clicked.
to get a reference to the containing <tr> element,
get the parent of the parent (in this case case <tr>)
*/
var oRow = src.parentElement.parentElement;
//once the row reference is obtained, delete it passing in its rowIndex
document.all("tblGrid").deleteRow(oRow.rowIndex);
}
</script>
</head>
<body>
<!-- sample table grid with 3 columns and 4 rows that are presented by default -->
<table id="tblGrid" style="table-layout:fixed">
<tr>
<td width="150px">Field1</td>
<td width="150px">Field2</td>
<td width="250px">Field3</td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
</table>
<hr>
<input type="button" value="Add Row" onClick="addRow();" />
<hr>
</body>
</html>
<head>
<title>js增加td,tr</title>
<script language="javascript">
//add a new row to the table
function addRow()
{
//add a row to the rows collection and get a reference to the newly added row
var newRow = document.all("tblGrid").insertRow();
//add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes
var oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t1'>";
oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t2'>";
oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t3'> <input type='button' value='Delete'
onclick='removeRow(this);'/>";
}
//deletes the specified row from the table
function removeRow(src)
{
/* src refers to the input button that was clicked.
to get a reference to the containing <tr> element,
get the parent of the parent (in this case case <tr>)
*/
var oRow = src.parentElement.parentElement;
//once the row reference is obtained, delete it passing in its rowIndex
document.all("tblGrid").deleteRow(oRow.rowIndex);
}
</script>
</head>
<body>
<!-- sample table grid with 3 columns and 4 rows that are presented by default -->
<table id="tblGrid" style="table-layout:fixed">
<tr>
<td width="150px">Field1</td>
<td width="150px">Field2</td>
<td width="250px">Field3</td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
<tr>
<td><input type="text" name="t1" /></td>
<td><input type="text" name="t2" /></td>
<td><input type="text" name="t3" /> <input type="button" value="Delete"
onClick="removeRow(this);" /></td>
</tr>
</table>
<hr>
<input type="button" value="Add Row" onClick="addRow();" />
<hr>
</body>
</html>
相关文章推荐
- Js-动态控制table的tr、td增加及删除的具体实现
- js动态控制table的tr,td增加及删除
- js动态控制table的tr、td增加及删除的具体实现
- js动态控制table的tr,td增加及删除
- js动态控制table的tr、td增加及删除的具体实现
- js动态控制table的tr,td增加及删除
- js动态增加,删除td,tr,table,div
- table-td中的删除按钮控制删除一行tr
- 简单的HTML中TAB分页实现,js控制页面tr行新增、删除、修改
- 运用js动态操作table(新增,删除相关列信息)
- js动态给table添加/删除tr
- js中获取 table节点各tr及td的内容方法
- js动态给table添加/删除tr
- js动态给table添加/删除tr的方法
- 用js操作table、tr、td 「字体样式及TD背景图片」
- 用js操作table(tr,td) (包括TD内容的隐藏,背景颜色和字体的设置)
- js中获取 table节点各tr及td的内容方法
- js动态控制表单的tr,td的显示和隐藏
- js控制table tr 的方法
- 运用js动态操作table(新增,删除相关列信息)