事件对象
默认事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="https://www.csdn.net/">CSDN</a>
<script>
{
var a=document.querySelector('a');
a.addEventListener('click',function(e){
// e 事件对象
console.log(e);
})
}
</script>
</body>
</html>
这里因为a标签有默认跳转事件,因此刚打印完毕事件对象e,就马上执行跳转了。
这里a默认行为是浏览器给的,浏览器给的功能都是默认事件(默认行为)。
我们要想看事件对象,就需要阻止默认行为!!
{
var a=document.querySelector('a');
a.addEventListener('click',function(e){
// e 事件对象
console.log(e);
// 阻止浏览器默认行为!
e.preventDefault();
})
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="https://www.csdn.net/">a标签</a>
<script>
{
var a=document.querySelector('a');
a.addEventListener('click',function(e){
alert("a标签!");
})
}
</script>
</body>
</html>
先执行事件监听中事件处理函数,再执行a标签的默认事件。
{
var a=document.querySelector('a');
a.addEventListener('click',function(e){
alert("a标签!");
// 阻止浏览器默认行为
e.preventDefault();
})
}
{
var a=document.querySelector('a');
a.addEventListener('click',function(e){
alert('a');
e.preventDefault();
},{
passive:true
});
}
事件监听函数的第三个参数 还可以 传递对象:{}
capture:true/false 是否在捕获阶段执行
once:true/false 只执行一次事件
passive:false/true //阻止取消默认事件
取消事件监听
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
background: crimson;
}
</style>
</head>
<body>
<div></div>
<input type="button" value="取消事件">
<script>
{
var div = document.querySelector('div');
var inp = document.querySelector("input");
div.addEventListener('click', function(){
alert("点击事件");
});
inp.addEventListener('click', function(){
div.removeEventListener('click', function(){
alert("点击事件");
});
});
}
</script>
</body>
</html>
我们发现取消了事件监听,但没有任何效果。
removeEventListener(event,fn)
绑定的事件函数-必须是命名函数!(不能是匿名函数)
{
var div = document.querySelector('div');
var inp = document.querySelector("input");
function fn(){
alert('点击事件')
}
div.addEventListener('click', fn);
inp.addEventListener('click', function(){
div.removeEventListener('click', fn);
});
}
Event事件对象
<!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: 200px;
height: 200px;
background: red;
margin: 100px auto;
}
p{
width: 100px;
height: 100px;
background: blue;
}
</style>
</head>
<body>
<div>
<p></p>
</div>
<script>
var div=document.querySelector('div');
var p=document.querySelector('p');
// 事件函数中有一个默认参数ev(事件对象)
div.addEventListener('click',function(e){
console.log(e);
})
</script>
</body>
</html>
形参e:事件对象 - 存储了许多和这个事件相关的属性!
e.target 事件触发的目标源元素
var div=document.querySelector('div');
var p=document.querySelector('p');
// 事件函数中有一个默认参数ev(事件对象)
div.addEventListener('click',function(e){
// console.log(e);
console.log(e.target);
})
没给p标签添加点击事件,但是点击之后就执行了。因为这里其实就是事件冒泡,点击子元素,就相当于点击了父元素,然后它会一层一层的往上找,找到父元素有点击事件,那就会执行父元素的点击事件。但这里的target拿到的是事件触发的目标元素,这里主要看目标源。
(后续待补充)