javaScript--05 DOM基础 12.11
2016-07-17 08:58
465 查看
<!DOCTYPE html>
<html lang="zh" id="htmlId">
<head id="headId">
<title>BOM基础,复制、克隆节点</title>
<meta charset="utf-8">
<meta name="keywords" content=",,">
<meta name="description" content="">
<style>
div{
text-align: center;
}
table{
display: inline-block;
border: 2px solid red;
}
.addButton
{
display: block;
margin: 0 auto;
}
</style>
</head>
<script type="text/javascript">
function addTable(){
var name = document.getElementById("name");
var email = document.getElementById("email");
var age = document.getElementById("age");
if(name.value == "" || email.value == "" || age.value == "")
{
return;
}
var thead = document.getElementsByTagName("tbody");
thead = thead[0];
var newTr = document.createElement("tr");
var newTd1 = document.createElement("td");
newTd1.innerText = name.value;
var newTd2 = document.createElement("td");
newTd2.innerText = email.value;
var newTd3 = document.createElement("td");
newTd3.innerText = age.value;
var newTd4 = document.createElement("td");
newTd4.innerHTML = "<button onclick = 'deleteThisTd(this)'>删除</button>";
newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
newTr.appendChild(newTd3);
newTr.appendChild(newTd4);
thead.appendChild(newTr);
name.value = "";
email.value = "";
age.value = "";
}
function deleteThisTd(td)
{
td.parentNode.parentNode.parentNode.removeChild(td.parentNode.parentNode);
}
</script>
<body id="bodyId">
<div>
<span>姓名:</span>
<input type="text" id="name" value="茉莉花">
<span>Email:</span>
<input type="text" id="email" value="molihua@flower.cn">
<span>年龄:</span>
<input type="text" id="age" value="38">
</div>
<br>
<input class="addButton" onclick="addTable()" type="button" value="添加">
<hr>
<div>
<table>
<thead>
<tr>
<th>姓名</th><th>邮箱</th><th>年龄</th><th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</body>
</html>
<html lang="zh" id="htmlId">
<head id="headId">
<title>BOM基础,复制、克隆节点</title>
<meta charset="utf-8">
<meta name="keywords" content=",,">
<meta name="description" content="">
<style>
div{
text-align: center;
}
table{
display: inline-block;
border: 2px solid red;
}
.addButton
{
display: block;
margin: 0 auto;
}
</style>
</head>
<script type="text/javascript">
function addTable(){
var name = document.getElementById("name");
var email = document.getElementById("email");
var age = document.getElementById("age");
if(name.value == "" || email.value == "" || age.value == "")
{
return;
}
var thead = document.getElementsByTagName("tbody");
thead = thead[0];
var newTr = document.createElement("tr");
var newTd1 = document.createElement("td");
newTd1.innerText = name.value;
var newTd2 = document.createElement("td");
newTd2.innerText = email.value;
var newTd3 = document.createElement("td");
newTd3.innerText = age.value;
var newTd4 = document.createElement("td");
newTd4.innerHTML = "<button onclick = 'deleteThisTd(this)'>删除</button>";
newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
newTr.appendChild(newTd3);
newTr.appendChild(newTd4);
thead.appendChild(newTr);
name.value = "";
email.value = "";
age.value = "";
}
function deleteThisTd(td)
{
td.parentNode.parentNode.parentNode.removeChild(td.parentNode.parentNode);
}
</script>
<body id="bodyId">
<div>
<span>姓名:</span>
<input type="text" id="name" value="茉莉花">
<span>Email:</span>
<input type="text" id="email" value="molihua@flower.cn">
<span>年龄:</span>
<input type="text" id="age" value="38">
</div>
<br>
<input class="addButton" onclick="addTable()" type="button" value="添加">
<hr>
<div>
<table>
<thead>
<tr>
<th>姓名</th><th>邮箱</th><th>年龄</th><th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</body>
</html>
相关文章推荐
- JavaScript--05 DOM基础 12.9 12.10 复制、克隆节点
- JavaScript传递变量:值传递?引用传递?
- 编写高性能JavaScript
- 学习Javascript的8张思维导图
- JavaScript实现异步的4种方法
- Javascript闭包深入解析及实现方法
- JavaScript模板引擎的应用场景及实现原理
- 编写高性能的JavaScript事件
- JavaScript数组操作函数方法详解
- JavaScript事件冒泡、事件捕获和阻止默认事件
- 强类型 JavaScript 的解决方案
- JavaScript模块化开发的那些事
- JavaScript String 对象实例深入研究
- JavaScript高级特性 — 作用域
- 细说JavaScript单线程的一些事
- JSONP 安全攻防技术(JSON劫持、 XSS漏洞)
- jstl 中的 包含标签<c:import>
- JSTL中类似if else的标签使用例子
- 之前见汤姆大叔 写过一系列的 js 深入理解 呢 很是感觉经典
- Ajax跨域:Jsonp原理解析