本文主要是介绍document.getElementById(addUser).onclick=function(){,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户</title>
</head>
<body>
<center>
<br><br>
添加用户:<br><br>
姓名: <input type="text" name="name" id="name" />
email: <input type="text" name="email" id="email" />
电话: <input type="text" name="tel" id="tel" /><br><br>
<button id="addUser">提交</button>
<br><br>
<hr>
<br><br>
<table id="usertable" border="1" cellpadding="5" cellspacing=0>
<tbody>
<tr>
<th>姓名</th>
<th>email</th>
<th>电话</th>
<th> </th>
</tr>
<tr>
<td>Tom</td>
<td>tom@tom.com</td>
<td>5000</td>
<td><a href="deleteEmp?id=Tom">Delete</a></td>
</tr>
<tr>
<td>Jerry</td>
<td>jerry@sohu.com</td>
<td>8000</td>
<td><a href="deleteEmp?id=Jerry">Delete</a></td>
</tr>
</tbody>
</table>
</center>
</body>
<script language="JavaScript">
//<button id="addUser">提交</button>
document.getElementById("addUser").οnclick=function(){
/*
* 姓名: <input type="text" name="name" id="name" />
email: <input type="text" name="email" id="email" />
电话: <input type="text" name="tel" id="tel" /><br><br>
*/
var nameValue=document.getElementById("name").value;
var emailValue=document.getElementById("email").value;
var telValue=document.getElementById("tel").value;
/*
* <tr>
<td>Jerry</td>
<td>jerry@sohu.com</td>
<td>8000</td>
<td><a href="deleteEmp?id=Jerry">Delete</a></td>
</tr>
*/
var trelem=document.createElement("tr");
var nameelem=document.createElement("td");
var nameText=document.createTextNode(nameValue);
nameelem.appendChild(nameText);
var emailelem=document.createElement("td");
var emailText=document.createTextNode(emailValue);
emailelem.appendChild(emailText);
var telelem=document.createElement("td");
var telText=document.createTextNode(telValue);
telelem.appendChild(telText);
var atdelem=document.createElement("td");
var a=document.createElement("a");
a.setAttribute("href","deleteEmp?id="+nameValue);
var delText=document.createTextNode("Delete");
a.appendChild(delText);
atdelem.appendChild(a);
trelem.appendChild(nameelem);
trelem.appendChild(emailelem);
trelem.appendChild(telelem);
trelem.appendChild(atdelem);
var table=document.getElementById("usertable");
var tbody=document.createElement("tbody");
// alert("tfy");
//在除IE外其他浏览器,在页面中查找不到tbody标签
tbody.appendChild(trelem);
table.appendChild(tbody);
// alert("tfy");
a.οnclick=function (){
/*
* <tr>
<td>Jerry</td>
<td>jerry@sohu.com</td>
<td>8000</td>
<td><a href="deleteEmp?id=Jerry">Delete</a></td>
</tr>
*/
var trelem=a.parentNode.parentNode;
var name=trelem.firstChild.firstChild.nodeValue;
var flag=confirm("您确认要删除"+name+"用户吗?");
if(!flag){
//取消
/*
* 当点击按钮时,页面会有自动的提交动作,
* 因为没有设置提交的页面,所以显示"找不到网页"
*
* * return false的作用:
* * 禁止页面刷新
* * 禁止页面自动提交的动作
*/
return false;
}else{
//确认
var parentele=trelem.parentNode;
parentele.removeChild(trelem);
return false;
}
}
}
</script>
</html>
这篇关于document.getElementById(addUser).onclick=function(){的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!