练习一:实现如下功能,点击添加获取到输入框的值,添加一行数据,点击删除,删除一行数据,并实现各行换色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态表格</title>
<style>
table {
border: 1px solid;
margin: auto;
width: 500px;
}
td,
th {
text-align: center;
border: 1px solid;
}
div {
text-align: center;
margin: 50px;
}
#div3 {
text-align: center;
margin-top: 10px;
}
.out {
background-color: white;
}
.over {
background-color: pink;
}
</style>
<script>
window.onload = function() {
//给添加按钮绑定单击事件btn_add
document.getElementById("btn_add").onclick = function() {
//获取输入框的值(id,name,gender)
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var gender = document.getElementById("gender").value;
if(id == "") {
alert("id不能为空");
return;
}
if(name == "") {
alert("name不能为空");
return;
}
if(gender == "") {
alert("gender不能为空");
return;
}
//获取table
var table = document.getElementById("table");
//使用innerHTML属性往table中来设置行
table.innerHTML += "<tbody><tr>\n" +
"<td><input type=\"checkbox\" name=\"ck\"/></td>" +
" <td>" + id + "</td>\n" +
" <td>" + name + "</td>\n" +
" <td>" + gender + "</td>\n" +
" <td><a href=\"javascript:void(0);\" οnclick=\"del(this)\">删除</a></td>\n" +
" </tr></tbody>";
//innerHTML追加之后,会重新绘制table,所以在添加按钮事件中,在执行一次,选择最上面复选框实现全选
document.getElementById("input_th").onclick = function(ev) {
var ele_cks = document.getElementsByName("ck");
for(var i = 0; i < ele_cks.length; i++) {
ele_cks[i].checked = this.checked;
}
};
//innerHTML追加之后,会重新绘制table,所以在添加按钮事件中,在执行一次实现各行变色
var trs = document.getElementsByTagName("tr");
for(var i = 0; i < trs.length; i++) {
//移到元素之上
trs[i].onmouseover = function() {
this.className = "over";
}
//移出元素
trs[i].onmouseout = function() {
this.className = "out";
}
}
}
//选择最上面复选框实现全选
document.getElementById("input_th").onclick = function() {
var ele_cks = document.getElementsByName("ck");
for(var i = 0; i < ele_cks.length; i++) {
ele_cks[i].checked = this.checked;
}
};
//全不选
document.getElementById("input_2").onclick = function() {
var ele_cks = document.getElementsByName("ck");
for(var i = 0; i < ele_cks.length; i++) {
ele_cks[i].checked = false;
}
}
//反选
document.getElementById("input_3").onclick = function() {
var ele_cks = document.getElementsByName("ck");
for(var i = 0; i < ele_cks.length; i++) {
if(ele_cks[i].checked) {
ele_cks[i].checked = false;
} else {
ele_cks[i].checked = true;
}
}
}
//全选
document.getElementById("input_1").onclick = function() {
var ele_cks = document.getElementsByName("ck");
for(var i = 0; i < ele_cks.length; i++) {
ele_cks[i].checked = true;
}
}
//实现各行变色
var trs = document.getElementsByTagName("tr");
for(var i = 0; i < trs.length; i++) {
//移到元素之上
trs[i].onmouseover = function() {
this.className = "over";
}
//移出元素
trs[i].onmouseout = function() {
this.className = "out";
}
}
}
//删除一行
var del = function(obj) {
//获取table节点
var table = obj.parentNode.parentNode.parentNode;
//获取table子节点tr
var tr = obj.parentNode.parentNode;
table.removeChild(tr);
}
</script>
</head>
<body>
<div>
<form action="#" method="get">
<input type="text" id="id" placeholder="请输入编号">
<input type="text" id="name" placeholder="请输入姓名">
<input type="text" id="gender" placeholder="请输入性别">
<input type="button" value="添加" id="btn_add">
<input type="reset" value="重置" />
</form>
</div>
<table id="table">
<caption>学生信息表</caption>
<tr>
<th><input id="input_th" type="checkbox" name="ck" /></th>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr>
<td><input type="checkbox" name="ck" /></td>
<td>1</td>
<td>令狐冲</td>
<td>男</td>
<td>
<a href="javascript:void(0);" οnclick="del(this)">删除</a>
</td>
</tr>
<tr>
<td><input type="checkbox" name="ck" /></td>
<td>2</td>
<td>任我行</td>
<td>男</td>
<td>
<a href="javascript:void(0);" οnclick="del(this)">删除</a>
</td>
</tr>
<tr>
<td><input type="checkbox" name="ck" /></td>
<td>3</td>
<td>岳不群</td>
<td>?</td>
<td>
<a href="javascript:void(0); " οnclick="del(this)">删除</a>
</td>
</tr>
</table>
<div id="div3">
<input class="input3" type="button" id="input_1" value="全选">
<input class="input3" type="button" id="input_2" value="全不选">
<input class="input3" type="button" id="input_3" value="反选">
</div>
</body>
</html>
练习二、注册表单校验