一.单击事件案例
var btn = document.getElementsByTagName("button")[0];
btn.onclick = function(){
//获取button的文本值.
var text = btn.innerText ;
if(text=="开始点名"){
btn.innerText = "停止点名"
}else{
btn.innerText = "开始点名";
}
}
二.双击事件案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
div{
width: 230px;
height: 230px;
text-align: center;
border : 1px solid black;
display: none;
}
</style>
</head>
<body>
<button>双击打开登录页面</button>
<div>
<h3>登录弹窗!!</h3>
账号<input type="text" placeholder="请输入你的账号" /> <br>
密码<input type="password" placeholder="请输入你的密码" /> <br><br><br>
<button>登录</button> <button>返回</button>
</div>
<script>
//双击打开登录页面
document.getElementsByTagName("button")[0].ondblclick = function(){
document.getElementsByTagName("div")[0].style.display = "block";
}
document.getElementsByTagName("button")[2].onclick = function(){
document.getElementsByTagName("div")[0].style.display = "none";
}
</script>
</body>
</html>
三.鼠标移入移出事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.a{
width: 200px;
height: 200px;
}
.b{
width: 400px;
height: 400px;
}
</style>
</head>
<body>
<img class="a" src="img/1.PNG">
<script>
//鼠标移入图片内部 ,图片放大 鼠标移出图片 图片还原
var img = document.getElementsByTagName("img")[0];
img.onmouseover = function(){
// img.style.width = "400px";
// img.style.height = "400px";
img.className = "b";
}
img.onmouseout = function(){
// img.style.width = "200px";
// img.style.height = "200px";
img.className = "a";
}
</script>
</body>
</html>
四.键盘输入事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
小写字母:<input type="text" id="a" /> <br>
大写字母:<input type="text" id="b" />
<script>
//在第一个文本框输入小写字母.下面的文本框默认输出上面的与之对应的大写字母
//思路:给第一个文本框加上键盘输入事件(每敲击一下键盘就触发一下事件).
//获取第一个文本框的值.然后赋予给第二个文本框
var i1 = document.getElementById("a");
var i2 = document.getElementById("b");
i1.onkeyup = function(){
i2.value = i1.value.toUpperCase();
}
</script>
</body>
</html>
五.获取焦点和失去焦点事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
账号:<input type="text" id="a" /> <span id="c"></span> <br>
<script>
var username = document.getElementById("a");
var password = document.getElementById("a");
var tip1 = document.getElementById("c");
var tip2 = document.getElementById("d");
//需求:输入账号之后.提示账号输入规则.
//思路给文本框加上获取焦点事件 给第一个span标签生产一个提示用于
username.onfocus = function(){
tip1.innerText = "账号的长度要在6-10之间";
tip1.style.color = "red";
}
//需求2:输入完成之后,要判断账号是否符合格式.
//思路:失去焦点之后判断.
username.onblur = function(){
var username123 = username.value;
if (username123.length >=6 && username123.length<=10) {
tip1.innerText = "√";
tip1.style.color = "green";
} else{
tip1.innerText = "X";
tip1.style.color = "red";
}
}
</script>
</body>
</html>
六.表单提交事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form id="f" action="#" method="post">
账号:<input type="text" id="a" /> <span id="c"></span> <br>
密码:<input type="password" id="b" /> <span id="d"></span> <br>
<input type="submit" value="登录" />
</form>
<script>
var username = document.getElementById("a");
var password = document.getElementById("b");
var tip1 = document.getElementById("c");
var tip2 = document.getElementById("d");
var f = document.getElementById("f");
//需求:输入账号之后.提示账号输入规则.
//思路给文本框加上获取焦点事件 给第一个span标签生产一个提示用于
username.onfocus = function(){
tip1.innerText = "账号的长度要在6-10之间";
tip1.style.color = "red";
}
//需求2:输入完成之后,要判断账号是否符合格式.
//思路:失去焦点之后判断.
username.onblur = function(){
var username123 = username.value;
if (username123.length >=6 && username123.length<=10) {
tip1.innerText = "√";
tip1.style.color = "green";
} else{
tip1.innerText = "X";
tip1.style.color = "red";
}
}
password.onfocus = function(){
tip2.innerText = "密码的长度要在6-10之间";
tip2.style.color = "red";
}
//需求2:输入完成之后,要判断账号是否符合格式.
//思路:失去焦点之后判断.
password.onblur = function(){
var password123 = password.value;
if (password123.length >=6 && password123.length<=10) {
tip2.innerText = "√";
tip2.style.color = "green";
} else{
tip2.innerText = "X";
tip2.style.color = "red";
}
}
//onsubmit和其他的事件有个不同之处. 需要返回值的默认是return true表示提交 return false就表示不提交
f.onsubmit = function(){
if(tip1.innerText=="√" && tip2.innerText=="√" ){
return true;
}else{
alert("请按照要求写");
return false;
}
}
</script>
</body>
</html>
七.下拉菜单事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<select id="a">
<option>中国银行</option>
<option>建设银行</option>
<option>交通银行</option>
</select>
<select id="b">
<option>中国银行卡号111</option>
<option>建设银行卡号222</option>
<option>交通银行卡号333</option>
</select>
<script>
// onchange事件: 一般只会作用于下拉框 点击下拉框且改变值.
//适用场景: 银行卡的互动. 地址栏的联动
var a = document.getElementById("a");
var b = document.getElementById("b");
//需求实现两个下拉框的联动效果.
//思路:给第一个下拉框加上一个onchange事件
//思路:获取第一个下拉框的第几个元素(下标)
//思路:将上一步的思路的下标 赋予给第二个下拉框即可
a.οnchange= function(){
// 如何获取下拉框的下标
// console.log(a.selectedIndex);
b.selectedIndex = a.selectedIndex;
}
b.οnchange= function(){
// 如何获取下拉框的下标
// console.log(a.selectedIndex);
a.selectedIndex = b.selectedIndex;
}
</script>
</body>
</html>
八.修改元素的属性
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<img src="img/1.PNG" />
<button>替换图片</button>
<script>
//html元素是有很多属性的.
//例如 id class name src href
document.getElementsByTagName("button")[0].onclick = function(){
//将xxx元素的aaa属性改成bbb
//xxx.setAttribute("aaa","bbb");
//var a = xxx.getAttribute("aaa");表示xxx的aaa属性的值
// document.getElementsByTagName("img")[0].setAttribute("src","img/2.PNG");
if(document.getElementsByTagName("img")[0].getAttribute("src") == "img/1.PNG" ){
document.getElementsByTagName("img")[0].setAttribute("src","img/2.PNG");
}else{
document.getElementsByTagName("img")[0].setAttribute("src","img/1.PNG");
}
}
</script>
</body>
</html>
九.简化版的轮播图
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
img{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<img src="img/1.PNG" />
<script>
//只要有需求: 每隔一段事件就要作什么事 就需要联想到使用定时器
var img = document.getElementsByTagName("img")[0];
//实现图片的替换 但是图片的文件名可能不是有规律.
//所以可以将图片的所有文件名路径名 存到数组中. 数组是有规律的.
var src_1 = ["img/1.PNG","img/2.PNG","img/3.PNG","img/4.PNG"];
var index = 1;
setInterval(function(){
img.setAttribute("src",src_1[index]);
index++;
if(index==4){
index = 0;
}
},1000);
</script>
</body>
</html>