注册事件
1.注册事件
a.传统注册on开头的事件注册时,具有唯一性。同一个元素只能绑定一个事件,后面会把前面的覆盖。
b.方法监听注册方式
<!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>
<button>传统注册</button>
<button>方法监听注册</button>
<script>
var btns = document.querySelectorAll('button');
// 传统注册具有唯一性
btns[0].onclick = function() {
alert('hello');
}
// 把上面覆盖掉
btns[0].onclick = function() {
alert('hi')
}
// 方法监听注册事件,不会覆盖
btns[1].addEventListener('click', function() {
alert(2);
})
btns[1].addEventListener('click', function() {
alert(3);
})
// ie特有btns[2].attachEvent()
</script>
</body>
</html>
2.删除、解绑事件
当我们不需要绑定事件时,可以进行解绑。
传统:elemen.onclick = null;
方法监听事件:removeEventListener()
<!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 {
background-color: blue;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<script>
var div = document.querySelectorAll('div');
// 传统解绑
div[0].onclick = function() {
alert('e1');
div[0].onclick = null;
}
// 方法监听绑定事件解绑
div[1].addEventListener('click', fn);
function fn() {
alert('eee!');
div[1].removeEventListener('click', fn);
}
</script>
</body>
</html>
3.DOM事件流
事件传播的过程,从外层一点一点找
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>
div {
width: 100px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div>123</div>
<script>
// 事件对象
var div = document.querySelector('div');
div.onclick = function(e) {
// console.log(e);
// console.log(window.event);
// e = e || window.event;
console.log(e);
}
// div.addEventListener('click', function(e) {
// console.log(e);
// })
// 1. event 就是一个事件对象 写到我们侦听函数的 小括号里面 当形参来看
// 2. 事件对象只有有了事件才会存在,它是系统给我们自动创建的,不需要我们传递参数
// 3. 事件对象 是 我们事件的一系列相关数据的集合 跟事件相关的 比如鼠标点击里面就包含了鼠标的相关信息,鼠标坐标啊,如果是键盘事件里面就包含的键盘事件的信息 比如 判断用户按下了那个键
// 4. 这个事件对象我们可以自己命名 比如 event 、 evt、 e
// 5. 事件对象也有兼容性问题 ie678 通过 window.event 兼容性的写法 e = e || window.event;
</script>
</body>
</html>
常用事件对象的属性
event.target,触发事件的元素,this是绑定事件的元素
5.阻止默认行为
比如我们有一个链接,点击时我不想让他跳转,就可以先阻止默认行为
<!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>
<a href="www.baidu.com">百度</a>
<script>
// 阻止跳转
var a = document.querySelector('a');
a.addEventListener('click', function(e) {
e.preventDefault();
})
// a.onclick = function(e) {
// e.preventDefault();
// return false; 无兼容性问题
// }
</script>
</body>
</html>
6.事件委托
<!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>
</head>
<body>
<ul>
<li>知否知否,点我应有弹框在手!</li>
<li>知否知否,点我应有弹框在手!</li>
<li>知否知否,点我应有弹框在手!</li>
<li>知否知否,点我应有弹框在手!</li>
<li>知否知否,点我应有弹框在手!</li>
</ul>
<script>
// 事件委托的核心原理:给父节点添加侦听器, 利用事件冒泡影响每一个子节点
var ul = document.querySelector('ul');
ul.addEventListener('click', function(e) {
// alert('知否知否,点我应有弹框在手!');
// e.target 这个可以得到我们点击的对象
e.target.style.backgroundColor = 'pink';
})
</script>
</body>
</html>
7.常用鼠标事件
特殊:菜单事件:contentmenu
选中事件: selectstart
8.鼠标事件对象
MouseEvent
案例:
鼠标跟随天使
<!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="D:\java-script_learning\image\angel.gif" alt="">
<script>
var img= document.querySelector('img');
document.addEventListener('mousemove', function(e) {
var x = e.pageX;
var y = e.pageY;
console.log(x + y);
img.style.left = x - 50 + 'px';
img.style.top = y - 40 + 'px';
})
</script>
</body>
</html>
9.键盘事件
<!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>
<script>
// 弹起时触发,按着不松开不触发
document.onkeyup = function() {
console.log('d');
}
// 按下触发顺序大于press
document.onkeydown = function() {
console.log('m');
}
// 不能识别功能键
document.onkeypress = function() {
console.log('d');
}
</script>
</body>
</html>
键盘事件对象
<!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>
<script>
document.onkeyup = function(e) {
console.log(e.keyCode);
}
</script>
</body>
</html>
案例按键输入
<!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>
<input type="text">
<script>
var input = document.querySelector('input');
document.addEventListener('keyup', function(e) {
if (e.keyCode === 65) {
input.focus();
}
})
</script>
</body>
</html>
变大案例
<!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>
* {
padding: 0;
margin: 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>
var input = document.querySelector('.jd');
var big = document.querySelector('.con');
input.addEventListener('keyup', function() {
if(this.value == "") {
big.style.display = 'none';
}else {
big.style.display = 'block';
big.innerHTML = this.value;
}
})
input.addEventListener('focus', function() {
if (this.value !== '') {
big.style.display = 'block';
}
})
input.addEventListener('blur', function() {
big.style.display = 'none';
})
</script>
</body>
</html>