表格操作二例

来源:互联网 发布:matlab 读取数据 编辑:程序博客网 时间:2024/05/16 15:20

例一:

<BODY>
<SCRIPT LANGUAGE="JavaScript">
function addRow(tbl)
{
var a = tbl.insertRow();
a.id = "a" + tbl.rows.length;
for(var i=0; i<tbl.rows[0].cells.length; i++)
{
var tc = a.insertCell();
tc.innerText = " ";
tc.onfocus = function()
{
this.parentElement.parentElement.parentElement.ct = this.parentElement;
this.innerHTML = "<input type=/"text/" value=/"" + (this.innerText==" "?"":this.innerText) + "/" style=/"width:100%;height:100%;border:0;/" onblur=/"this.parentElement.innerText=(this.value==''?' ':this.value);/">";
this.children[0].focus();
};
}
}
function delRow(tbl)
{
if(!tbl.ct) return;
tbl.deleteRow(tbl.ct.rowIndex);
tbl.ct = null;
}
</SCRIPT>
<table border=1 id="tbl">
<tr id=a1>
<td>northsnow</td>
<td>塞北的雪</td>
</tr>
</table>
<input type="button" value="添加行" onclick="addRow(document.all.tbl);">
<input type="button" value="删除行" onclick="delRow(document.all.tbl);">
</BODY>

例二:


<table border=1>
<tr id=a1>
<td><input id=text1 name=text1 value="northsnow"></td><td><input id=text2 name=text2

value=塞北的雪></td>
</tr>
</table>
<input type=button name=ok value="add row" onclick=add()>
<script language=JavaScript>
i=1
function add(){
var newTR = a1.cloneNode(true);
newTR.id="a"+(++i)
a1.parentNode.insertAdjacentElement("beforeEnd",newTR);
}
</script>

原创粉丝点击