案例:(表单验证)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面+表单验证(用户名密码)</title>
<style>
input:focus {
outline: none;
box-shadow: 0 0 20px #6df4a0;
}
select:focus {
outline: none;
box-shadow: 0 0 20px #6df4a0;
}
textarea:focus {
outline: none;
box-shadow: 0 0 20px #6df4a0;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: url("../image/bg.jpg") no-repeat center center;
background-size: cover;
background-attachment: fixed;
background-color: #CCCCCC;
}
.rg_layout {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 900px;
height: 650px;
border-radius: 10px;
background-color: rgba(0, 0, 0, 0.6);
padding-top: 15px;
box-shadow: 0 0 20px #0007f3;
margin-top: -20px;
/*opacity:0.5; 该透明度影响整体*/
}
.rg_left {
float: left;
margin: 15px;
}
.rg_center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 500px;
margin-top: -20px;
/*float: left;*/
}
.rg_right {
float: right;
margin: 15px;
}
.rg_right p {
color: white;
}
.rg_right a {
color: cyan;
}
.p_reg_1 {
font-size: 20px;
color: #ffd026;
}
.p_reg_2 {
font-size: 20px;
color: #a6a6a6;
}
.td_left {
color: white;
width: 100px;
}
.td_right {
color: white;
padding-left: 15px;
vertical-align: middle;
}
#username, #password, #email, #realname, #phone, #date, #desc, #verify_code {
border: 1px solid #a6a6a6;
border-radius: 5px;
padding-left: 10px;
width: 260px;
height: 35px;
margin-top: 10px;
}
#province, #hobby, #date, #gender {
margin-top: 10px;
}
#img_vertify {
vertical-align: middle;
height: 30px;
width: 110px;
margin-left: 10px;
}
#sb_btn, #re_btn {
width: 50px;
height: 30px;
background-color: #ffd026;
border: 1px solid #ffd026;
border-radius: 5px;
}
</style>
<script>
window.onload = function () {
document.getElementById("form").onsubmit = function () {
//验证用户名
//验证密码
//...
//都成功则返回true
//
return checkUsername() && checkPassword();
}
//失去焦点后判断格式是否正确
// document.getElementById("username").onblur = checkUsername;
// document.getElementById("password").onblur = checkPassword;
//或者内容改变一次就判断一次格式是否正确
document.getElementById("username").oninput = checkUsername;
document.getElementById("password").oninput = checkPassword;
}
function checkUsername() {
var username = document.getElementById("username").value;
var reg_username = /^[a-zA-Z0-9]{6,12}$/;
var flag = reg_username.test(username);
var s_username = document.getElementById("span_username");
if (flag) {
s_username.innerHTML = "<img height='32' width='32' style='margin-left: 5px' src='../image/right.png'>"
} else {
s_username.innerHTML = "<img height='32' width='34' src='../image/wrong.png' style='margin-left: 5px'> <br> <font color='red'style='font-size: xx-small'>用户名格式错误(正确格式为6-12位字母或数字)</font>"
}
return flag;
}
function checkPassword() {
var password = document.getElementById("password").value;
var reg_password = /^[a-zA-Z0-9]{6,12}$/;
var flag = reg_password.test(password);
var s_password = document.getElementById("span_password");
if (flag) {
s_password.innerHTML = "<img height='32' width='32' src='../image/right.png'>"
} else {
s_password.innerHTML = "<img height='32' width='34' src='../image/wrong.png' style='margin-left: 5px'> <br> <font color='red'style='font-size: xx-small'>密码格式错误(正确格式为6-12位字母或数字)</font>"
}
return flag;
}
</script>
</head>
<body>
<div class="rg_layout">
<div class="rg_left">
<p class="p_reg_1">新用户注册</p>
<p class="p_reg_2">USER REGISTER</p>
</div>
<div class="rg_right">
<p>已有账号?<a href="#">立即登录</a></p>
</div>
<div class="rg_center" align="center">
<div class="rg_form">
<form id="form" action="#" method="get">
<table border="0" cellpadding="1px" cellspacing="0" align="center" width="100%">
<tr>
<td align="right" class="td_left">
<label for="username">用户名:</label>
</td>
<td align="left" class="td_right">
<input id="username" type="text" name="username" placeholder="请输入用户名">
<sapn id="span_username" style="vertical-align: middle"></sapn>
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="password">密码:</label>
</td>
<td align="left" class="td_right">
<input id="password" type="password" name="password" placeholder="请输入密码">
<sapn id="span_password" style="vertical-align: middle"></sapn>
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="email">Email:</label>
</td>
<td align="left" class="td_right">
<input id="email" type="email" name="email" placeholder="请输入Email">
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="realname">姓名:</label>
</td>
<td align="left" class="td_right">
<input id="realname" type="text" name="realname" placeholder="请输入姓名">
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="phone">手机号:</label>
</td>
<td align="left" class="td_right">
<input id="phone" type="text" name="phone" placeholder="请输入手机号">
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="gender">性别:</label>
</td>
<td align="left" class="td_right">
<input id="gender" type="radio" name="gender" value="男" checked="checked"><label
for="gender">男</label>
<input type="radio" name="gender" value="女" id="girl" style="margin-left: 10px"><label
for="girl">女</label>
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="date">出生日期:</label>
</td>
<td align="left" class="td_right">
<input id="date" type="date" name="date">
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="province">地址:</label>
</td>
<td align="left" class="td_right">
<select id="province" name="province" style="height: 35px; border-radius: 5px">
<option value="">--请选择--</option>
<option value="1">北京</option>
<option value="2">上海</option>
</select>
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="hobby">爱好:</label>
</td>
<td align="left" class="td_right">
<input id="hobby" type="checkbox" name="h1" value="滑板"> <label for="hobby">滑板</label>
<input id="gun" type="checkbox" name="h2" value="枪械" style="margin-left: 10px"><label
for="gun">枪械</label>
<input id="programming" type="checkbox" name="h3" value="编程"
style="margin-left: 10px"><label for="programming">编程</label>
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="desc">个性签名:</label>
</td>
<td align="left" class="td_right">
<textarea id="desc" name="desc" placeholder="请输入个性签名" rows="3" cols="10"></textarea>
</td>
</tr>
<tr>
<td align="right" class="td_left">
<label for="verify_code">验证码:</label>
</td>
<td align="left" class="td_right">
<input type="text" id="verify_code" name="verify_code" placeholder="请输入验证码">
<img id="img_vertify" src="../image/verify_code.jpg"/>
</td>
</tr>
<tr align="center">
<td colspan="3">
<br>
<input id="sb_btn" type="submit" value="注册">
<input id="re_btn" type="reset" value="重置">
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
效果:
案例:(轮播图)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>轮播图小案例</title>
<style>
div{
border: 1px solid blue;
margin: auto;
width: 40%;
height: 300px;
}
img{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<img id="img" align="center" src="../image/banner_1.jpg">
</div>
<script>
var i=1;
var img=document.getElementById("img");
setInterval(fun,1000);
function fun() {
img.src="../image/banner_"+i+".jpg" // ../ 代表当前目录的上一级目录
i++;
if(i>3){
i=1;
}
}
</script>
</body>
</html>
效果:
案例(自动跳转到我的主页)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Location对象</title>
</head>
<body>
<p align="center"><font id="second" color="red">5</font>秒后跳转到我的主页...</p>
<script>
var ss = 5;
var second = document.getElementById("second");
function fun() {
second.innerHTML = ss-- + '';
if (ss === 0) {
location.href = "http://www.kinggm520.cn";
stop.clearInterval();
}
}
var stop = setInterval(fun, 1000);
</script>
</body>
</html>
效果:
案例(表格的动态添加和删除)
<!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;
}
</style>
</head>
<body>
<div>
<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">
</div>
<table cellspacing="0" border="1px">
<caption>学生信息表</caption>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</tr>
</table>
<script>
/*
//方式一、直接添加
分析:
1.添加:
1. 给添加按钮绑定单击事件
2. 获取文本框的内容
3. 创建td,设置td的文本为文本框的内容。
4. 创建tr
5. 将td添加到tr中
6. 获取table,将tr添加到table中
2.删除:
1.确定点击的是哪一个超链接
<a href="javascript:void(0);" οnclick="delTr(this);" >删除</a>
2.怎么删除?
removeChild():通过父节点删除子节点
*/
//1.获取按钮
/* document.getElementById("btn_add").onclick = function(){
//2.获取文本框的内容
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var gender = document.getElementById("gender").value;
//3.创建td,赋值td的标签体
//id 的 td
var td_id = document.createElement("td");
var text_id = document.createTextNode(id);
td_id.appendChild(text_id);
//name 的 td
var td_name = document.createElement("td");
var text_name = document.createTextNode(name);
td_name.appendChild(text_name);
//gender 的 td
var td_gender = document.createElement("td");
var text_gender = document.createTextNode(gender);
td_gender.appendChild(text_gender);
//a标签的td
var td_a = document.createElement("td");
var ele_a = document.createElement("a");
ele_a.setAttribute("href","javascript:void(0);");
ele_a.setAttribute("onclick","delTr(this);");
var text_a = document.createTextNode("删除");
ele_a.appendChild(text_a);
td_a.appendChild(ele_a);
//4.创建tr
var tr = document.createElement("tr");
//5.添加td到tr中
tr.appendChild(td_id);
tr.appendChild(td_name);
tr.appendChild(td_gender);
tr.appendChild(td_a);
//6.获取table
var table = document.getElementsByTagName("table")[0];
table.appendChild(tr);
}*/
//方式二、使用innerHTML添加
document.getElementById("btn_add").onclick = function() {
//2.获取文本框的内容
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var gender = document.getElementById("gender").value;
//获取table
var table = document.getElementsByTagName("table")[0];
//追加一行
table.innerHTML += "<tr>\n" +
" <td>"+id+"</td>\n" +
" <td>"+name+"</td>\n" +
" <td>"+gender+"</td>\n" +
" <td><a href=\"javascript:void(0);\" οnclick=\"delTr(this);\" >删除</a></td>\n" + //注意转义 \"
" </tr>";
}
//删除方法
function delTr(obj){
var table = obj.parentNode.parentNode.parentNode;
var tr = obj.parentNode.parentNode;
table.removeChild(tr);
}
</script>
</body>
</html>
效果:
案例(表格全选 反选)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表格全选</title>
<style>
table {
text-align: center;
}
.tr_out {
background-color: white;
}
.tr_over {
background-color: #1fbaf3;
}
</style>
<script>
//在页面加载完之后再绑定事件
window.onload = function () {
//给全选按钮绑定单击事件
document.getElementById("selectAll").onclick = function () {
var cbs = document.getElementsByName("cb");
var cb_all = document.getElementsByName("cb_first")[0];
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = true;
}
cb_all.checked = true;
}
//给全不选选按钮绑定单击事件
document.getElementById("selectNone").onclick = function () {
var cbs = document.getElementsByName("cb");
var cb_all = document.getElementsByName("cb_first")[0];
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = false;
}
cb_all.checked = false;
}
//给反选按钮绑定单击事件
document.getElementById("selectReverse").onclick = function () {
var cbs = document.getElementsByName("cb");
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = !cbs[i].checked; //将当前状态取反即可实现反选
}
}
//给第一个checkBox 设置全选和全不选功能
document.getElementsByName("cb_first")[0].onclick = function () {
var cb_first = document.getElementsByName("cb_first")[0];
var cbs = document.getElementsByName("cb");
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = cb_first.checked; //设置每一个cb的状态和第一个cb状态相同
}
}
//给所有tr绑定鼠标移入元素之上事件和移出事件
var trs = document.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
trs[i].onmouseover = function () { //移入
this.className = "tr_over";
}
trs[i].onmouseout = function () { //移出
this.className = "tr_out";
}
}
}
//删除 这个函数要放在 window.onload 之外才有效
function del(obj) {
var table = obj.parentNode.parentNode.parentNode;
var tr = obj.parentNode.parentNode;
table.removeChild(tr);
}
</script>
</head>
<body>
<table border="1px" align="center" cellspacing="0" cellpadding="1px" width="400px">
<caption>学生信息表</caption>
<tr>
<th>
<input type="checkbox" name="cb_first">
</th>
<th>
编号
</th>
<th>
姓名
</th>
<th>
性别
</th>
<th>
编号
</th>
</tr>
<tr>
<td>
<input type="checkbox" name="cb">
</td>
<td>
1
</td>
<td>
秀逗
</td>
<td>
男
</td>
<td>
<a href="javascript:void(0)" onclick="del(this);">删除</a>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="cb">
</td>
<td>
2
</td>
<td>
黄黄
</td>
<td>
女
</td>
<td>
<a href="javascript:void(0)" onclick="del(this);">删除</a>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="cb">
</td>
<td>
3
</td>
<td>
四眼
</td>
<td>
男
</td>
<td>
<a href="javascript:void(0)" onclick="del(this);">删除</a>
</td>
</tr>
</table>
<div align="center" style="margin-top: 20px">
<input id="selectAll" type="button" value="全选">
<input id="selectNone" type="button" value="全不选">
<input id="selectReverse" type="button" value="反选">
</div>
</body>
</html>
效果:
案例(提交数据到表格select二级联动)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html作业</title>
<style>
.t {
margin-left: 40px;
}
.s {
height: 30px;
width: 150px;
}
input {
width: 100%;
height: 25px;
}
.b {
background: #1fbaf3;
color: white;
border: 0;
height: 40px;
width: 100px;
margin-top: 20px;
font-size: large;
}
.d {
margin: 40px;
}
.tr {
height: 40px;
background: #F3F3F3;
text-align: center;
font-weight: bold;
}
.tr1 {
height: 40px;
text-align: center;
}
</style>
<script language="JavaScript">
var city = new Array;
city['北京市'] = ['朝阳区', '西城区', '东城区'];
city['河北省'] = ['石家庄', '唐山', '邯郸'];
city['山东省'] = ['济南', '泰安', '青岛'];
function allCity() {
var select1 = document.getElementById("a1");
for (var i in city) { //这里注意遍历数组的写法
select1.add(new Option(i, i), null);
}
addOption(); // 初始化选项
}
function addOption() {
var select2 = document.getElementById("a2");
var select1 = document.getElementById("a1").value;
select2.length = 0; //每次都先清空一下市级菜单
if (select1 != '-请选择省份-') {
select2.add(new Option("-请选择城市-", "0"), null);
for (var i in city[select1]) {
select2.add(new Option(city[select1][i], city[select1][i]), null);
}
} else if (select1 == '-请选择省份-') {
select2.length = 0;
select2.add(new Option("-请选择城市-", "0"), null);
}
}
window.onload = allCity();
</script>
</head>
<body>
<font> <b>添加地址:</b> </font>
<br>
<br>
<div class="t">
<form method="post">
<table cellpadding="4px">
<tr>
<td>所在地区:</td>
<td><select class="s" id="a1" onchange="addOption()" name="province">
<option value="0">-请选择省份-</option>
<option value="北京市">北京市</option>
<option value="河北省">河北省</option>
<option value="山东省">山东省</option>
</select></td>
<td><select class="s" id="a2" name="city">
<option value="0">-请选择城市-</option>
</select></td>
</tr>
<tr>
<td>详细地址:</td>
<td colspan="2"><input id="a3" type="text" name="address" required="required"></td>
</tr>
<tr>
<td>收货人姓名:</td>
<td colspan="2"><input id="a4" type="text" name="name"></td>
</tr>
<tr>
<td>联系电话:</td>
<td colspan="2"><input id="a5" type="text" name="phone"></td>
</tr>
<tr>
<td><input id="btn_add" class="b" type="button" value="保存"></td>
</tr>
</table>
</form>
</div>
<div class="d">
<table id="table" cellspacing="0" border="1" width="100%">
<tr class="tr">
<td>收货人</td>
<td>所在地区</td>
<td>详细地址</td>
<td>手机/电话</td>
<td>操作</td>
</tr>
</table>
<script>
var btn = document.getElementById("btn_add");
btn.onclick = function () {
// //获取文本框的内容
var area = document.getElementById("a1").value + document.getElementById("a2").value;
var detailArea = document.getElementById("a3").value;
var name = document.getElementById("a4").value;
var phone = document.getElementById("a5").value;
//获取table
var table = document.getElementById("table");
//追加一行
if(!(detailArea.trim()===""||detailArea==null)) {
table.innerHTML += "<tr class='tr1'>\n" +
" <td>" + name + "</td>\n" +
" <td>" + area + "</td>\n" +
" <td>" + detailArea + "</td>\n" +
" <td>" + phone + "</td>\n" +
" <td> <a href=\"javascript:void(0);\" οnclick=\"defAddr(this);\" >默认地址</a> </td>\n" +
" </tr>";
}else {
alert("详细地址不能为空!")
}
}
function defAddr(obj) {
alert("\n已添加为默认地址");
}
</script>
</div>
</body>
</html>
效果: