//删除表格 function delTable() { var theTable = document.getElementById("table1"); // 所在表格的ID while(theTable.hasChildNodes()) // 删除表格 { theTable.removeChild(theTable.lastChild); }
}
function insRow() // 动态创建表格, 插入table,row,cols, {
var theTable = document.getElementById("tableFujian"); var hidtempValue = document.getElementById("hidTempValue"); var hidUrlValue = document.getElementById("hidFujianUrlString");
if(hidFujianString != "" ) { var str= hidFujianString.split(","); delTable(); //清理数据
for( var i=0;i<= str.length; i++ ) { document.getElementById("table1").style.display = ""; var theRow = theTable.insertRow();
<html>
<body>
<table id="t">
<tr>
<td> </td>
</tr>
</table>
<script type="text/javascript">
var table = document.getElementById("t");
var td = document.createElement("td");
td.innerHTML = "测试内容";
table.rows[0].appendChild(td);
</script>
</body>
</html>
//删除表格
function delTable()
{
var theTable = document.getElementById("table1"); // 所在表格的ID
while(theTable.hasChildNodes()) // 删除表格
{
theTable.removeChild(theTable.lastChild);
}
}
function insRow() // 动态创建表格, 插入table,row,cols,
{
var theTable = document.getElementById("tableFujian");
var hidtempValue = document.getElementById("hidTempValue");
var hidUrlValue = document.getElementById("hidFujianUrlString");
if(hidFujianString != "" )
{
var str= hidFujianString.split(",");
delTable(); //清理数据
for( var i=0;i<= str.length; i++ )
{
document.getElementById("table1").style.display = "";
var theRow = theTable.insertRow();
if( i == 0 )
{
theCell = theRow.insertCell();
theCell.style.backgroundColor = "#cccccc";
theCell.height="5px";
theCell.width ="300px";
theCell.innerHTML = "名称";
theCell.align = "center";
theCell.value =i; var theCell = theRow.insertCell();
theCell.style.backgroundColor = "#cccccc";
theCell.height="5px";
theCell.width ="200px";
theCell.innerHTML = "操 作";
theCell.align = "center";
theCell.value = i;
}
else
{
theCell = theRow.insertCell();
theCell.style.backgroundColor = "#ccccff";
theCell.innerHTML = i;
theCell.align = "center";
theCell.value = i;
theCell = theRow.insertCell();
theCell.style.backgroundColor = "#ccccff";
theCell.innerHTML ="名称";
theCell.align = "center";
theCell.value = i;
}
}
}
}
--使用jquery,更省事:
<script src="jQuery.js"></script>
<input type="button" value='增加一个单元格' onclick="javascript:add();"/>
<table id="t" >
<tr id="tr1">
<td> aa </td>
</tr>
</table>
<script>
var i = 1;
function add()
{
var htmls="<td>我是新加的</td>";
$("tr").append(htmls);
}
</script>
<tr>
<td> 1</td>
</tr>
</table>
<script type="text/javascript">function aaa()
{
var table = document.getElementById("t");
var td = document.createElement("td");
td.innerHTML = "测试内容<a href='javascript:ccc(this)'>delete</a>";
table.rows[0].appendChild(td);
}function ccc(r)
{
var s=document.getElementById('t');
//alert(s);
var lostTab=s.removeChild(s.lastChild);
}</script>
</body>