练习1:模态框
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: #f6c2d2;
}
.box2 {
width: 500px;
height: 500px;
background-color: white;
position: absolute;
top: 100px;
left: 50%;
margin-left: -250px;
text-align: center;
line-height: 500px;
color: red;
}
.box3 {
position: absolute;
right: 0;
top: 0;
width: 50px;
height: 50px;
background-color: red;
color: white;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<input type="button" value="弹出模态框" id="btn">
<script>
var btn=document.getElementById("btn")
btn.onclick=function () {
var box1=document.createElement("div")
var box2=document.createElement("div")
var box3=document.createElement("div")
box2.innerText="弹出的模态框"
box3.innerText="X"
box1.classList.add("box1")
box2.classList.add("box2")
box3.classList.add("box3")
box1.appendChild(box2)
box2.appendChild(box3)
var body=document.getElementsByTagName('body')[0]
body.appendChild(box1)
box3.onclick=function () {
body.removeChild(box1)
}
}
</script>
</body>
</html>
练习2:模态框扩展
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: pink;
display: none;
}
.box2 {
width: 500px;
height: 500px;
background-color: white;
position: absolute;
top: 100px;
left: 50%;
margin-left: -250px;
line-height: 30px;
padding: 141px;
box-sizing: border-box;
}
.box3 {
width: 80px;
height: 80px;
background-color: red;
position: absolute;
right: 0;
top: 0;
text-align: center;
line-height: 80px;
color: white;
}
</style>
</head>
<body>
<input type="button" value="弹出模态框" id="btn">
<div class="box1">
<div class="box2">
<form action="">
<p>
用户名:<input type="text" name="username">
</p>
<p>
密码:<input type="password" name="password">
</p>
<p>
<input type="button" value="提交">
</p>
<div class="box3">X</div>
</form>
</div>
</div>
<script>
var btn=document.getElementById("btn")
var box1=document.getElementsByClassName("box1")[0]
btn.onclick=function () {
box1.style.display="block"
}
var box3=document.getElementsByClassName("box3")[0]
box3.onclick=function () {
box1.style.display="none"
document.getElementsByName("username")[0].value=""
document.getElementsByName("password")[0].value=""
}
</script>
</body>
</html>
练习3:点击有惊喜
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: red;
margin: auto;
text-align: center;
line-height: 200px;
font-size: 30px;
color: white;
}
</style>
</head>
<body>
<div class="box">
点击有惊喜!
</div>
<script>
var box=document.getElementsByClassName("box")[0]
var count=0
box.onclick=function () {
count++
if (count % 4 == 1){
this.style.backgroundColor="green"
this.innerText="继续点击!"
}else if(count % 4 == 2){
this.style.backgroundColor="yellow"
this.innerText="精彩即将揭晓"
}else if(count % 4 == 3){
this.style.backgroundColor="pink"
this.innerText="骗你的傻逼"
}else {
this.style.backgroundColor="red"
this.innerText="点击有惊喜!"
}
}
</script>
</body>
</html>
练习四 :简易评论框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box1 {
background-color: #f6c2d2;
width: 600px;
}
ul>li {
list-style: none;
background-color: #f5deb3;
border: 1px dotted black;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="box1">
<p>评论区:</p>
<ul></ul>
</div>
<div class="box2">
<p>留言内容</p>
<textarea name="" id="content" cols="30" rows="10"></textarea>
<p>
<input type="button" value="提交" id="btn">
<input type="button" value="统计" id="cal">
</p>
</div>
<script>
var btn=document.getElementById("btn")
var count=0
btn.onclick = function () {
var content=document.getElementById("content")
var val=content.value
if (val.length != 0){
var ul=document.getElementsByTagName("ul")[0]
var li=document.createElement("li")
var p1=document.createElement("p")
var p2=document.createElement("p")
count=document.getElementsByTagName("li").length+1
var d=new Date()
p1.innerHTML="#"+ count + "楼" + " " +d.toLocaleString() + " " +"<span class='del'>删除</span>"
p2.innerText=val
li.appendChild(p1)
li.appendChild(p2)
ul.appendChild(li)
content.value=""
var delButtons=document.getElementsByClassName("del")
var currentButton=delButtons[delButtons.length-1]
currentButton.onclick=function () {
ul.removeChild(this.parentNode.parentNode)
}
}
}
</script>
</body>
</html>
练习5:简易评论框改进
方式一:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#comment {
background-color: #b0b0b0;
width: 500px;
}
#comment ul li {
list-style: none;
background-color: wheat;
border: 1px dashed #000;
margin: 0px 10px 10px;
word-break: break-all;
word-wrap: break-word;
}
</style>
</head>
<body>
<div id="comment">
<p>评论内容:</p>
</div>
<div id="box">
<p>留言内容:</p>
<textarea name="" id="content" cols="30" rows="10"></textarea>
<p>
<input type="button" value="提交" id="btn">
<input type="button" value="统计" id="calculate">
</p>
</div>
<script>
var comment = document.getElementById('comment');
var box = document.getElementById('box');
var submit = document.getElementById('submit');
var content = document.getElementById('content');
var btn = document.getElementById('btn');
var calculate=document.getElementById('calculate');
var ul = document.createElement('ul');
comment.appendChild(ul);
var count=0;
btn.onclick = function () {
var val = content.value;
if (val.length != 0) {
var date = new Date();
var subTime = date.toLocaleString();
var li = document.createElement('li');
var p1 = document.createElement('h3');
var p2 = document.createElement('p');
var spans = document.getElementsByClassName('del');
count=spans.length+1;
p1.innerHTML = '#'+'<span class="num">'+count+'</span>'+'楼'+' '+subTime + '<span class="del"> 删除</span>';
p2.innerHTML = val;
li.appendChild(p1);
li.appendChild(p2);
ul.appendChild(li);
content.value = '';
}
function aa() {
var spans = document.getElementsByClassName('del');
for (var i = 0; i < spans.length; i++) {
spans[i].onclick=function (currentIndex) {
function bb() {
ul.removeChild(this.parentNode.parentNode);
count--;
var ss=document.getElementsByClassName('num');
for (var j=currentIndex;j<ss.length;j++){
ss[j].innerHTML=parseInt(ss[j].innerHTML)-1;
}
aa();
}
return bb;
}(i);
}
}
aa()
};
calculate.onclick = function () {
alert('一共发布了'+count+'条评论');
}
</script>
</body>
</html>
方式二:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box1 {
background-color: #f6c2d2;
width: 600px;
}
ul>li {
list-style: none;
background-color: #f5deb3;
border: 1px dotted black;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="box1">
<p>评论区:</p>
<ul></ul>
</div>
<div class="box2">
<p>留言内容</p>
<textarea name="" id="content" cols="30" rows="10"></textarea>
<p>
<input type="button" value="提交" id="btn">
<input type="button" value="统计" id="cal">
</p>
</div>
<script>
var btn=document.getElementById("btn")
var count=0
btn.onclick = function () {
var content=document.getElementById("content")
var val=content.value
if (val.length != 0){
var ul=document.getElementsByTagName("ul")[0]
var li=document.createElement("li")
var p1=document.createElement("p")
var p2=document.createElement("p")
count=document.getElementsByTagName("li").length+1
var d=new Date()
p1.innerHTML="#"+ "<span class='num'>"+count +"</span>"+ "楼" + " " +d.toLocaleString() + " " +"<span class='del'>删除</span>"
p2.innerText=val
li.appendChild(p1)
li.appendChild(p2)
ul.appendChild(li)
content.value=""
var delButtons=document.getElementsByClassName("del")
var currentButton=delButtons[delButtons.length-1]
currentButton.onclick=function () {
var allNum=document.getElementsByClassName("num")
var currntNum=this.previousElementSibling
for (var i=0;i<allNum.length;i++) {
if (currntNum == allNum[i]) {
for (var j=i+1;j<allNum.length;j++) {
allNum[j].innerText=parseInt(allNum[j].innerText) - 1
}
break
}
}
ul.removeChild(this.parentNode.parentNode)
count--
}
}
}
</script>
</body>
</html>
练习六:选项卡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 600px;
height: 400px;
border: 1px solid red;
margin: auto;
}
ul>li {
list-style: none;
width: 200px;
height: 80px;
background-color: gray;
text-align: center;
line-height: 80px;
float: left;
}
ul:after {
display: table;
content: "";
clear: both;
}
.content {
background-color: #f6c2d2;
width: 600px;
height: 320px;
display: none;
}
li.active {
background-color: #55bbbb;
}
div.active {
display: block;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li class="active">首页</li>
<li>新闻</li>
<li>图片</li>
</ul>
<div class="content active">
首页内容
</div>
<div class="content ">
新闻内容
</div>
<div class="content">
图片内容
</div>
</div>
<script>
var arr=document.getElementsByTagName('li')
for (var i=0;i<arr.length;i++) {
arr[i].n=i
arr[i].onclick=function () {
for (j=0;j<arr.length;j++) {
arr[j].classList.remove("active")
document.getElementsByClassName("content")[j].classList.remove("active")
}
this.classList.add('active')
document.getElementsByClassName("content")[this.n].classList.add("active")
}
}
</script>
</body>
</html>
练习七:用户名和密码校验
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
span {
background-color: red;
}
</style>
</head>
<body>
<form>
<p>
<input type="text" name="username">
<span></span>
</p>
<p>
<input type="password" name="password">
<span></span>
</p>
<p>
<input type="button" value="提交" id="btn">
</p>
</form>
<script>
var isOk1=false
var reg1=new RegExp('(?!^[0-9]+$)(?!^[a-zA-Z]+$)^[0-9A-Za-z]{4}$')
var inp1 = document.getElementsByName("username")[0]
inp1.onblur=function () {
var res=reg1.test(this.value)
this.style.border="1px solid red"
if (!res) {
this.nextElementSibling.innerText="用户名必须由4位字母和数字组成"
setTimeout(function () {
inp1.nextElementSibling.innerText=""
inp1.value=""
},3000)
}else {
this.style.border="1px solid green"
isOk1=true
}
}
var isOk2=false
var reg2=new RegExp('(?!^[0-9]+$)(?!^[a-zA-Z]+$)^[0-9A-Za-z]{6}$')
var inp2 = document.getElementsByName("password")[0]
inp2.onblur=function () {
var res=reg2.test(this.value)
this.style.border="1px solid red"
if (!res) {
this.nextElementSibling.innerText="密码必须由6位字母和数字组成"
setTimeout(function () {
inp2.nextElementSibling.innerText=""
inp2.value=""
},3000)
}else {
this.style.border="1px solid green"
isOk2=true
}
}
var btn=document.getElementById("btn")
btn.onclick=function () {
if(isOk1 && isOk2) {
alert("提交成功")
}else {
alert("请重新填写")
}
}
</script>
</body>
</html>
参考博客