写法一:(有点啰嗦)
//--------------XML DOM--------------------------------------
function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var trObj = document.createElement("tr");
var tdUsername = document.createElement("td");
var tdEmail = document.createElement("td");
var tdAge = document.createElement("td");
var tdOp = document.createElement("td");
//3.添加td的内容
tdUsername.innerHTML=username;
tdEmail.innerHTML=email;
tdAge.innerHTML=age;
tdOp.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>";
//4.搞关系
trObj.appendChild(tdUsername);
trObj.appendChild(tdEmail);
trObj.appendChild(tdAge);
trObj.appendChild(tdOp);
//5.把tr添加到表格中
document.getElementById("tabInfo").appendChild(trObj);
}
function deleteRowss(obj){
var trObjss = obj.parentNode.parentNode;//tr
trObjss.parentNode.removeChild(trObjss);
}
写法二:(推荐写法)
//---------------------HTML DOM---------------------------------
function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var tabinfoss = document.getElementById("tabInfo");
//在表格中添加一行
var trss = tabinfoss.insertRow();
var tdUsernamess = trss.insertCell();
var tdEmailss = trss.insertCell();
var tdAgess = trss.insertCell();
var tdOpss = trss.insertCell();
//3.添加td的内容
tdUsernamess.innerHTML=username;
tdEmailss.innerHTML=email;
tdAgess.innerHTML=age;
tdOpss.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>";
}
function deleteRowss(obj){
//1.得到表格
var tabinfoss = document.getElementById("tabInfo");
//2.找到当前行的索引 rowIndex
var rowindexss = obj.parentNode.parentNode.rowIndex;
//3.删除
tabinfoss.deleteRow(rowindexss);//当前行的索引
}
//--------------------------------------------------------------
写法三 :IE中 貌似有问题
function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var tabinfoss = document.getElementById("tabInfo");
tabinfoss.innerHTML+="<tr><td>"+username+"</td><td>"+email+"</td><td>"+age+"</td><td><input type='button' value='删除' onclick='deleteRowss(this)'/></td></tr>";
}
function deleteRowss(obj){
//1.得到表格
var tabinfoss = document.getElementById("tabInfo");
//2.找到当前行的索引 rowIndex
var rowindexss = obj.parentNode.parentNode.rowIndex;
//3.删除
tabinfoss.deleteRow(rowindexss);//当前行的索引
}
//-------------------------------------------------------------------HTML 部分---------------------------------------------------------------//
<body>
<table align="center" cellpadding="10" cellspacing="3" >
<tr>
<td>姓名:<input type="text" id="username"/></td>
<td>Email:<input type="text" id="email"/></td>
<td>年龄:<input type="text" id="age"/></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="button" value="添加" onclick="addTR()"/></td>
</tr>
</table>
<br/>
<br/>
<br/>
<hr/>
<table id="tabInfo" align="center" width="500" border="1">
<tr>
<td>姓名</td>
<td>Email</td>
<td>年龄</td>
<td>操作</td>
</tr>
</table>
</body>