一.事件的绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件绑定方式</title>
<style type="text/css">
.box{
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<button class="begin">开始</button>
<button class="event_1">事件绑定1</button>
<button class="event_2">事件绑定2</button>
<!--点击开始,重新获得点击事件(所有状态重置)-->
<script>
// 每一个box点击都会toggle颜色, 当颜色都变成黑色, 取消所有点击事件,
// 点击开始, 重新获得点击事件(所有状态应该重置)
var beginBtn=document.querySelector(".begin");
var boxs=document.querySelectorAll(".box");
//给开始按钮绑定一个重置事件
beginBtn.onclick=init;
//定义一个计数器,记录变黑的个数
var count=0;
//点击box切换颜色
function toggleColor(){
// console.log(this)
if (this.style.backgroundColor=="yellow"){
this.style.backgroundColor="black"
count+=1;
}
else{
this.style.backgroundColor="yellow"
count-=1;
}
//检测是否需要结束
count==3&&overAction();
}
//结束功能,取消点击事件
function overAction(){
for (var i = 0; i < boxs.length; i++){
boxs[i].onclick=null;
}
}
//重置功能
function init() {
for (var i = 0; i < boxs.length; i++) {
boxs[i].style.backgroundColor = "yellow"; //将所有box背景重置为yellow
boxs[i].onclick=toggleColor; //绑定点击事件
}
//计数器重置
count=0;
}
//启动服务
init();
</script>
二.事件的绑定方式
1.事件绑定的第一种方式:on事件绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件绑定方式</title>
<style type="text/css">
.box{
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 50%;
}
</style>
</head>
<body>
<button class="event_1">事件绑定1</button>
<button class="event_2">事件绑定2</button>
<script>
//事件绑定的第一种方式:on事件绑定
var event_on_1=document.querySelector(".event_1");
event_on_1.onclick=function(){
console.log("事件绑定方式1")
}
</script>
</body>
</html>
2.事件绑定的第二种方式:非on事件绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件绑定方式</title>
<style type="text/css">
.box{
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 50%;
}
</style>
</head>
<body>
<button class="event_1">事件绑定1</button>
<button class="event_2">事件绑定2</button>
<script>
var event_on_2=document.querySelector(".event_2");
event_on_2.addEventListener("click",function(){
console.log("事件绑定方式2-a")
}) //“click”:为事件名
event_on_2.addEventListener("click",function(){
console.log("事件绑定方式2-b")
})
var action=function(){
console.log("事件绑定方式2-b")
}
event_on_2.addEventListener("click",action)
//取消绑定事件
event_on_2.removeEventListener("click",action);
</script>
</body>
</html>
总结:1.第一种只能绑定一个实现体, 如果有多次绑定, 保留最后一次
2.box.onclick = null来取消事件的绑定
3.第二种能绑定多个实现体, 如果有多次绑定, 按顺序依次执行
4.var fn = function() {} ;box.addEventListener('click', fn)
5.box.removerEventListener('click', fn)来取消事件的绑定
6.了解: 第三个参数决定冒泡顺序(子父级谁先相应事件)
三. 事件对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件对象</title>
<style type="text/css">
body{
margin: 0;
}
.box{
width: 100px;
height: 200px;
background-color: red;
}
.sup{
width: 200px;
height: 200px;
background-color: #0f8209;
}
.sub{
width: 100px;
height:100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box">hahahah</div>
<div class="sup">
<div class="sub"></div>
</div>
<a href="http://www.baidu.com" class="aBtn">只响应自身点击事件</a>
<script>
var box=document.querySelector(".box");
//事件的钩子函数,系统回调时
box.onclick=function(ev){
//回调函数
console.log(ev)
//特殊按键:altKey|shiftKey|ctrKey
console.log(ev.altKey)
//鼠标的点击点
console.log(ev.clientX,ev.clientY)
};
var sup=document.querySelector(".sup");
var sub=document.querySelector(".sub");
//事件默认有冒泡,子级响应事件后,会将事件传递给父级相同的事件,也会被激活,最终传递给document
sub.onclick=function(ev){
console.log("子级被点击了")
console.log(ev)
//取消冒泡,当自身处理事件后,该事件就处理完毕,结束,不再向上传递
ev.cancelBubble=true;
};
sup.onclick=function(){
console.log("父级被点击了")
};
document.onclick=function () {
console.log("文档被点击了")
}
</script>
<script>
var aBtn=document.querySelector("a")
aBtn.onclick=function(ev){
ev.cancelBubble=true; //取消冒泡
console.log("a被点击了");
//手动转跳页面
open('http://www.oldboyedu.com',"_self");
//a标签默认会完成转跳,取消默认事件
return false;
}
</script>
</body>
</html>
总结: 1.系统回调事件函数时, 传递了一个 事件(event) 实参
2.如果要使用传递来的实参 事件(event), 定义接收的 形参 即可
3.box.onclick = function(ev){
// 使用事件对象
// 特殊按键 eg: ev.altKey: true | false
// 鼠标触发点: ev.clientX | ev.clientY
// 取消冒泡
ev.cancelBubber = true;
// 取消默认事件
return false;
}