目录
1、注册事件(绑定事件)
1.1、注册事件概述
1.2、addEventListener 事件监听方式
<body>
<button>传统注册事件</button>
<button>方法监听注册事件</button>
<script>
var btns = document.querySelectorAll('button');
btns[0].onclick = function(){
alert('hi');
}
btns[0].onclick = function(){
alert('hello');//弹出hello,将上面的hi覆盖掉了
}
//事件侦听注册事件 addEventListener
//里面的事件类型是字符串 必定加引号 而且不带on(onclick)
//同一个元素 同一个事件可以添加多个侦听器(事件处理程序)
btns[1].addEventListener('click',function(){
alert(22);
})
btns[1].addEventListener('click',function(){
alert(45);//弹出22,后弹出45,不会覆盖掉前面的
})
</script>
</body>
1.3、attachEvent事件监听方式(了解)
<body>
<button>attachEvent</button>
<script>
var btns = document.querySelectorAll('button');
// 3、attachEvent ie9以前的版本支持
btns[0].attachEvent('onclick',function(){
alert(11);
})
</script>
</body>
1.4、注册事件兼容性解决方案
2、删除事件(解绑事件)
2.1、删除事件的方式
2.2、删除事件兼容性解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
height: 50px;
width: 50px;
background-color: blue;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<script>
var divs = document.querySelectorAll('div');
divs[0].onclick = function(){
alert(11);
// 1、传统方式删除,解绑;
divs[0].onclick = null;
}
// 2、方法监听
divs[1].addEventListener('click',fn)//里面的fn 不需要调用加小括号
function fn(){
alert(22);
divs[1].removeEventListener('click',fn);
}
divs[2].attachEvent('onclick',fn1);
function fn1(){
alert(33);
divs[2].detachEvent('onclik',fn1);
}
</script>
</body>
</html>
3、DOM事件流
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
height: 300px;
width: 150px;
background-color: blue;
}
.son{
height: 200px;
width: 100px;
background-color: rgb(151, 162, 232);
}
</style>
</head>
<body>
<div class="father">
<div class="son">盒子</div>
</div>
<script>
// dom事件流 三个阶段
// 1、JS代码中只能执行捕获或者冒泡其中一个阶段
// 2、onclick 和 attachEvent(ie)只能得到冒泡阶段
// 3、捕获阶段 如果addEventListener 第三个参数是true 那么则处于捕获阶段
// 先捕获 document -> html -> body -> father -> son
var son =document.querySelector('.son');
son.addEventListener('click',function(){
alert('son');
},true);
var father =document.querySelector('.father');
father.addEventListener('click',function(){
alert('father');
},true);
// 4、冒泡阶段 如果addEventListener 第三个参数是false或者省略 那么处于冒泡阶段
// son -> father -> body -> html ->document
var son =document.querySelector('.son');
son.addEventListener('click',function(){
alert('son');
},false);
var father =document.querySelector('.father');
father.addEventListener('click',function(){
alert('father');
},false);
document.addEventListener('click',function(){
alert('document');
})
</script>
</body>
</html>
4、事件对象
4.1、事件对象概念
4.2、事件对象的使用语法
4.3、事件对象的兼容性方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>123</div>
<script>
var div = document.querySelector('div');
// 1、event是一个事件对象,写到监听函数的小括号中,当形参来看
// 2、事件对象只有了事件才会存在,它是系统自动创建的,不需要我们传递参数
// 3、事件对象 是事件的一系列相关数据的集合 跟事件相关的 比如鼠标点击就包含了鼠标相关信息
// 4、事件对象可以自己命名,例如event evt e
// div.onclick = function(event){
// console.log(event);
// }
// div.addEventListener('click',function(event){
// console.log(event);
// })
// 5、事件对象也有兼容性问题ie678通过window.event兼容性写法 e= e||window.event;
div.onclick = function(e){
console.log(e);
console.log(window.event);
e = e|| window.event;
}
</script>
</body>
</html>
4.4、事件对象的常见属性和方法
4.4.1、this和e.targer的区别
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>123</div>
<ul>
<li>abc</li>
<li>abc</li>
<li>abc</li>
</ul>
<script>
// 常见事件对象的属性和方法
// e.target返回的是出发事件的对象(元素) this返回的是绑定事件的对象(元素)
// 区别:e.target点击了哪个元素就返回哪个元素
// this哪个元素绑定了这个点击事件,就返回谁
var div = document.querySelector('div');
div.addEventListener('click',function(e){
console.log(e.target);
console.log(this);
})
var ul = document.querySelector('ul');
ul.addEventListener('click',function(e){
// 给ul绑定了事件, 那么this就指向ul
console.log(this);
// e.target指向我们点击的那个对象 谁触发了这个事件
// 我们点击了li e.target指向的就是li
console.log(e.target);
})
// 兼容性
// div.onclick = function(e){
// e = e||window.event;
// var target = e.target || e.srcElement;
// console.log(target);
// }
</script>
</body>
</html>
4.4.2、事件对象阻止默认行为
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>123</div>
<a href="http://www.baidu.com">百度</a>
<form action="http://www.baidu.com">
<input type="submit" value="提交" name="sub">
</form>
<script>
// 返回事件类型
var div = document.querySelector('div');
div.addEventListener('click',fn);
div.addEventListener('mouseover',fn);
div.addEventListener('mouseout',fn);
function fn(e){
console.log(e.type);
}
// 阻止事件行为(事件) 让链接不跳转 或让提交按钮不提交
var a =document.querySelector('a');
a.addEventListener('click',function(e){
e.preventDefault();//dom标准写法
})
// 传统的注册方式
a.onclick = function(e){
// 普通浏览器 e.preventDefault();
// e.preventDefault();
// 低版本浏览器 ie678 returnValue 属性
// e.returnValue;
// 利用return false 也能阻止默认行为 没有兼容性问题
// 特点:return 后面的代码不执行了,且仅限于传统的注册方式
return false;
alert(11);
}
</script>
</body>
</html>
5、阻止事件冒泡
5.1、阻止事件冒泡的两种方式
5.2、阻止事件冒泡的兼容性解决方案
6、事件委托(代理、委派)
<body>
<ul>
<li>前端工程师</li>
<li>前端工程师</li>
<li>前端工程师</li>
<li>前端工程师</li>
</ul>
<script>
// 事件委托的核心原理 给父节点添加侦听器 利用事件冒泡影响没一个子节点
var ul = document.querySelector('ul');
ul.addEventListener('click',function(e){
// alert('好好学习,天天向上');//点击任意一个li都会弹出弹窗
// e.target 可以得到我们点击的对象
e.target.style.backgroundColor = 'blue'; //点击到哪个对象,对象就会显示蓝色
})
</script>
</body>
7、常用的鼠标事件
7.1、常用的鼠标事件
7.2、鼠标事件对象
7.3、案例:跟随天使
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img{
position: absolute;
top:2px;
}
</style>
</head>
<body>
<img src="2.gif" alt="">
<script>
var pic = document.querySelector('img')
document.addEventListener('mousemove',function(e){
// mousemove只要我们鼠标移动1px,就会触发这个事件
// console.log(1);
// 核心原理:每次鼠标移动,我们都会获得最新的鼠标坐标
// 把这个x和y坐标作为图片的top和left值就可以移动图片
var x = e.pageX;
var y = e.pageY;
console.log('x坐标是'+x,'y坐标是'+y);
// 记得给left和top加px单位
// 图片默认跟在鼠标右边
pic.style.left=x + 'px';
pic.style.top=y + 'px';
})
</script>
</body>
</html>
图片想与鼠标居中,根据图片大小,坐标减半(图片大小:500×355)
修改其中的值就可以了
pic.style.left=x-250 + 'px';
pic.style.top=y-125 + 'px';
8.常用的键盘事件
8.1、常用的键盘事件
事件除了使用鼠标触发,还可以使用键盘触发
8.2、keyCode判断用户按下哪个键
8.3、案例:模拟京东按键输入内容
在京东页面按住s键,焦点自动在搜索框
body>
<input type="text">
<script>
var search = document.querySelector('input');
document.addEventListener('keyup',function(e){
if(e.keyCode===83){
search.focus();
}
})
</script>
</body>
8.4、案例:模拟京东快递单号查询
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.search {
position: relative;
width: 178px;
margin: 100px;
}
.con {
display: none;
position: absolute;
top: -40px;
width: 171px;
border: 1px solid rgba(0, 0, 0, .2);
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
padding: 5px 0;
font-size: 18px;
line-height: 20px;
color: #333;
}
.con::before {
content: '';
width: 0;
height: 0;
position: absolute;
top: 28px;
left: 18px;
border: 8px solid #000;
border-style: solid dashed dashed;
border-color: #fff transparent transparent;
}
</style>
</head>
<body>
<div class="search">
<div class="con">123</div>
<input type="text" placeholder="请输入您的快递单号" class="jd">
</div>
<script>
// 快递单号输入内容时, 上面的大号字体盒子(con)显示(这里面的字号更大)
// 表单检测用户输入: 给表单添加键盘事件
// 同时把快递单号里面的值(value)获取过来赋值给 con盒子(innerText)做为内容
// 如果快递单号里面内容为空,则隐藏大号字体盒子(con)盒子
var con = document.querySelector('.con');
var jd_input = document.querySelector('.jd');
jd_input.addEventListener('keyup', function() {
// console.log('输入内容啦');
if (this.value == '') {
con.style.display = 'none';
} else {
con.style.display = 'block';
con.innerText = this.value;
}
})
// 当我们失去焦点,就隐藏这个con盒子
jd_input.addEventListener('blur', function() {
con.style.display = 'none';
})
// 当我们获得焦点,就显示这个con盒子
jd_input.addEventListener('focus', function() {
if (this.value !== '') {
con.style.display = 'block';
}
})
</script>
</body>