一、案例:     【动态生成Table的行、列以及删除等】
二、案例类别: 【页面】
三、案例关键字:【Table】 【Row】 【TR】
四、案例模型:
       动态的生成Table。
五、案例解释:
      
六、案例代码:
       代码分成很多形式。现在只是介绍其中的几种:
       一)动态插入行:
              1、
function insert2tbl(x){
       R=tbl.insertRow();
       C=R.insertCell();
       C.innerHTML="<td width='18%'><div align=center><input type='checkbox' checked /></div>      </td>";
       C=R.insertCell() ;
       C.innerHTML="<td width='82%' id='td1'>"+x+"</td>" ;
      
}
此时每次执行向table中插入一条记录。
 
              2、
function AddRow()
{
//添加一行
var newTr = tab1.insertRow();
//添加两列
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
//设置列内容和属性
newTd0.innerHTML = '<input type=checkbox >';
newTd1.innerText= '新增加行';
}
              见 附1。
二)删除
       1、删除除了第一行外的其他数据
function clearTbl(){            
       while(tbl.rows.length>1){
              tbl.deleteRow();    
       }    
}
 
 
七、案例心得及经验总结:
       动态生成Table的行可以有利于我们与List嵌套使用。
 
八、实用案例:对应关系、动态选择
 
九、案例相关方法及拓展方法:

 
附1:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>动态添加删除表格</title>

<Script Language="Javascript">
var cGetRow=-99999;

function AddRow()
{
//添加一行
var newTr = tab1.insertRow();
//添加两列
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
//设置列内容和属性
newTd0.innerHTML = '<input type=checkbox >
</body>
</html>
 
附 2:
<HEAD>
<script LANGUAGE="JAVASCRIPT">
iIndex = 0; //试验一下加了int类型定义后如何???
var i= 0;
function showIndex(){
 alert(iIndex);
}
function getIndex(){
 iIndex = event.srcElement.parentElement.parentElement.rowIndex;
return iIndex;
}
function insertRow(iPos){
 var otr=myTable.insertRow(i);
 var ocell=otr.insertCell(0);
 ocell.innerHTML="<input type=file name=aa >"
 var ocell=otr.insertCell(1);
// ocell.innerText="bb"
 ocell.innerHTML=" <input type=button onclick=deleteRow(getIndex()) value='删除"+ i +"'>";
i++;
}
function deleteRow(iPos){
 document.all.myTable.deleteRow(iPos);
i--;
}
</SCRIPT>
</HEAD>
<BODY>
<table border=1 width=600 >
</table>
<form>
<input type=button onclick="alert(iIndex)" value="show Index">
<input type=button onclick="insertRow(0)"  value="插入行">
</form>
</BODY>
</HTML>
 
附 3:
<script>
function deleteRow (tableID, rowIndex) {
var table =document.all[tableID]
table.deleteRow(rowIndex);
}
</script>
<table id=mxh border=1>
<tr>
<td>第1行</td><td onclick="deleteRow('mxh',this.parentElement.rowIndex)">删除本行</td>
</tr>
<tr>
<td>第2行</td><td onclick="deleteRow('mxh',this.parentElement.rowIndex)">删除本行</td>
</tr>
<tr>
<td>第3行</td><td onclick="deleteRow('mxh',this.parentElement.rowIndex)">删除本行</td>
</tr>
<tr>
<td>第4行</td><td onclick="deleteRow('mxh',this.parentElement.rowIndex)">删除本行</td>
</tr>
</table>
 
 
<HEAD>
<script LANGUAGE="JAVASCRIPT">
iIndex = 0; //试验一下加了int类型定义后如何???
function showIndex(){
alert(iIndex);
}
 
function getIndex(){
iIndex = event.srcElement.parentElement.rowIndex;
}
function insertRow(iPos){
var otr=myTable.insertRow(iPos);
var ocell=otr.insertCell(0);
ocell.innerText="aa"
var ocell=otr.insertCell(1);
ocell.innerText="bb"
}
function deleteRow(iPos){
document.all.myTable.deleteRow(iPos);
}
</SCRIPT>
</HEAD>
<BODY>
<table border=1>
<tr onclick="getIndex()">
<td>1</td>
<td>2</td>
</tr>
<tr onclick="getIndex()">
<td>1</td>
<td>2</td>
</tr>
</table>
<form>
<input type=button onclick="alert(iIndex)" value="show Index">
<input type=button onclick="insertRow(iIndex)" value="插入行">
<input type=button onclick="deleteRow(iIndex)" value="删除行">
</form>
</BODY>
</HTML>

相关文章:

  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2021-07-27
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-27
  • 2021-10-10
  • 2021-12-14
  • 2021-10-02
  • 2021-09-02
  • 2022-12-23
  • 2021-11-11
相关资源
相似解决方案