<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<p>请输入姓名:<input type="text" id="text">
请输入年龄:<input type="text" id="age">
<input type="button" value="添加" onclick="add()">
</p>
<script>
var text=document.getElementById("text");
var age=document.getElementById("age");
var table=document.createElement("table");
table.border=1;
table.width="800";
table.cellSpacing="0";
var tbody=document.createElement("tbody");
table.appendChild(tbody);
var ten=document.createTextNode("ID");
var tena=document.createTextNode("姓名");
var tenb=document.createTextNode("年龄");
var tenc=document.createTextNode("操作");
tbody.insertRow(0);
tbody.rows[0].insertCell(0);
tbody.rows[0].cells[0].appendChild(ten);
tbody.rows[0].insertCell(1);
tbody.rows[0].cells[1].appendChild(tena);
tbody.rows[0].insertCell(2);
tbody.rows[0].cells[2].appendChild(tenb);
tbody.rows[0].insertCell(3);
tbody.rows[0].cells[3].appendChild(tenc);
document.body.appendChild(table);
var i=1;
function add(){
var j=0;
var ten=document.createTextNode(i);
var tena=document.createTextNode(text.value);
var tenb=document.createTextNode(age.value);
var tenc=document.createElement("input");
tenc.value="删除";
tenc.type="button";
tenc.onclick="del(this)";
tbody.insertRow(i);
tbody.rows[i].insertCell(j);
tbody.rows[i].cells[j].appendChild(ten);
tbody.rows[i].insertCell(j+1);
tbody.rows[i].cells[j+1].appendChild(tena);
tbody.rows[i].insertCell(j+2);
tbody.rows[i].cells[j+2].appendChild(tenb);
tbody.rows[i].insertCell(j+3);
tbody.rows[i].cells[j+3].appendChild(tenc);
i++;j++;
}
function del(obj){
var fix=obj.parentNode.parentNode.parentNode;
var ok=obj.parentNode.parentNode;
fix.removeChild(ok);
if(tbody.length==1){i=0}
}
</script>
</body>
</html>
相关分类