这篇文章介绍了js动态创建表格
删除行列的实例代码
有需要的朋友可以参考一下
复制代码 代码如下:
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>无标题文档</title>
<link rel="stylesheet" href="tabcss" />
<script type="text/javascript" src="/Day/doctooljs"></script>
<script type="text/javascript">
function createTab()
{
var tabNode = doccreateElement("table");
var tbdNode = doccreateElement("tbody");
var trNode = doccreateElement("tr");
var tdNode = doccreateElement("td");
var textNode = doccreateTextNode("单元格一");
tdNodeappendChild(textNode);
trNodeappendChild(tdNode);
tbdNodeappendChild(trNode);
tabNodeappendChild(tbdNode);
byTag("div")[]appendChild(tabNode);
}
function createTab()
{
var tabNode = doccreateElement("table");
//tabNodeid = "tabid";
tabNodesetAttribute("id""tabid");
var row = byName("rownum")[]value;
var col = byName("colnum")[]value;
for(var x=; x<=row; x++)
{
var trNode = tabNodeinsertRow();
for(var y=; y<=col; y++)
{
var tdNode = trNodeinsertCell();
tdNodeinnerHTML = x+""+y;
}
}
byTag("div")[]appendChild(tabNode);
eventsrcElementdisabled = true;
}
function delRow()
{
var tabNode = byId("tabid");
if(tabNode==null)
{
alert("表格不存在 ");
return;
}
var rownum = byName("delrow")[]value;
if(rownum> && rownum<=tabNoderowslength)
tabNodedeleteRow(rownum);
else
{
alert("删除的行不存在学习数数很重要");
}
}
function delCol()
{
var tabNode = byId("tabid");
if(tabNode==null)
{
alert("表格不存在 ");
return;
}
var colnum = byName("delcol")[]value;
if(colnum> && colnum<=tabNoderows[]cellslength)
{
for(var x=;x<tabNoderowslength; x++)
{
tabNoderows[x]deleteCell(colnum);
}
}
else
{
alert("删除的列不存在");
}
}
</script>
</head>
<body>
<!
通过页面的按钮可以动态的创建一个表格
>
行<input type="text" name="rownum" /><br />
列<input type="text" name="colnum" /><br />
<input type="button" value="创建表格" onclick="createTab()" /><br />
<input type="text" name="delrow" />
<input type="button" value="删除行" onclick="delRow()" />
<br />
<input type="text" name="delcol" />
<input type="button" value="删除列" onclick="delCol()" />
<br />
<br />
<div>
</div>
</body>
</html>