事件对象简介
event对象代表时间的状态,比如键盘按键的状态,鼠标的位置,鼠标按钮的状态
事件发生后,跟事件相关的一系列信息数据的集合都放导这个对象里面,对象就是事件对象event
<body>
<div>123</div>
<script>
var div = document.querySelector('div');
div.onclick = function(e) {
console.log(e);
console.log(window.event);//IE678
兼容性处理
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
</script>
</body>
事件对象的常见属性和方法
代码
e.target
<body>
<div>123</div>
<ul>
<li>abc</li>
<li>abc</li>
<li>abc</li>
</ul>
<script>
// 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指向点击的对象 谁触发了这个事件 e.target就指向谁
console.log(e.target);
})
// 兼容性写法
div.onclick = function(e) {
e = e || window.Event;
var target = e.target || e.srcElement;
console.log(target);
}
// 跟this非常相似的属性currentTarget ie678不认识(点击后返回ul)
</script>
</body>
e.type
<body>
<div>123</div>
<a href="http://www.baidu.com">百度</a>
<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);
}
</script>
</body>
e.preventDefault
<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();
})
//兼容性写法
a.onclick = function() {
//普通浏览器 e.preventDefault();
e.preventDefault();
//低版本浏览器 ie678
e.returnValue;
//利用return false 也能阻止默认行为 没有兼容性问题 特点:return后面的代码不执行
//而且只限于传统注册方式a.onclick
return false;
alert(11);
}
</script>
</body>
阻止事件冒泡的两种方式
事件冒泡:开始时由最具体的元素接受,然后逐级向上传播到DOM最顶层节点
标准写法:e.stopPropagation
<html>
<head>
<meta charset="utf-8">
<title>333333</title>
<style>
.father {
position: relative;
width: 200px;
height: 200px;
background-color: pink;
margin: 50% auto;
}
.son {
position: absolute;
width: 150px;
height: 150px;
background-color: purple;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
text-align: center;
line-height: 150px;
}
</style>
</head>
<body>
<div class="father">
<div class="son">son</div>
</div>
<script>
//阻止冒泡dom推荐的标准方法 stopPropagation();
var son = document.querySelector('.son');
son.addEventListener('click', function(e) {
alert('son');
e.stopPropagation();
},false);
var father = document.querySelector('.father');
father.addEventListener('click', function(e) {
alert('father');
},false);
document.addEventListener('click', function() {
alert('document');
})
</script>
</body>
</html>
非标准写法:e.cancelBubble
<html>
<head>
<meta charset="utf-8">
<title>333333</title>
<style>
.father {
position: relative;
width: 200px;
height: 200px;
background-color: pink;
margin: 50% auto;
}
.son {
position: absolute;
width: 150px;
height: 150px;
background-color: purple;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
text-align: center;
line-height: 150px;
}
</style>
</head>
<body>
<div class="father">
<div class="son">son</div>
</div>
<script>
//阻止冒泡dom推荐的标准方法 stopPropagation();
var son = document.querySelector('.son');
son.addEventListener('click', function(e) {
alert('son');
e.cancelBubble = true;// 非标准cancel取消bubble泡泡
},false);
var father = document.querySelector('.father');
father.addEventListener('click', function(e) {
alert('father');
},false);
document.addEventListener('click', function() {
alert('document');
})
</script>
</body>
</html>
兼容性解决方案
//兼容性解决方案
if(e && e.stopPropagation) {
e.stopPropagation();
}else {
window.event.cancelBubble = true;
}
事件委托(代理,委派)
事件委托也成为事件代理,再jQuery里面称为事件委派
原理
不是每个字节点单独设置监听器,而是将事件监听器设置在父节点上,利用冒泡原理影响每个子节点
作用
只用操作一个DOM,提高了程序的性能
代码
<body>
<ul>
<li>aaaaaa</li>
<li>aaaaaa</li>
<li>aaaaaa</li>
<li>aaaaaa</li>
<li>aaaaaa</li>
</ul>
<script>
var ul = document.querySelector('ul');
ul.addEventListener('click', function(e) {
// alert('aaaaa');
// e.target可以得到点击的对象
e.target.style.backgroundColor = 'pink';
})
</script>
</body>