JAVA全系列 教程
3762个小节阅读:7095.1k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
选择器
DOM操作
CSS操作
事件处理
遍历
动画
获取事件类型
xxxxxxxxxx
$("#btn").click("click",function(e){
console.log(e.type);
})
获取当前元素对象
xxxxxxxxxx
$("#btn").click("click",function(e){
console.log(e.target);
})
获取当前元素对象
温馨提示
target:指向触发事件元素
currentTarget:指向添加事件的元素
xxxxxxxxxx
<!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>
<script src="./js/jquery-3.6.0.min.js"></script>
<style>
div{
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="container">
<button id="btn">按钮</button>
</div>
<script>
$("#container").click("click",function(e){
console.log("container",e.currentTarget);
console.log("container",e.target);
})
$("#btn").click("click",function(e){
console.log("btn",e.currentTarget);
console.log("btn",e.target);
})
</script>
</body>
</html>
如果调用这个方法,默认事件行为将不再触发。
xxxxxxxxxx
<a href="https://itbaizhan.com">itbaizhan</a>
<script>
$("a").click("click",function(e){
e.preventDefault();
})
</script>
防止事件冒泡到DOM树上,也就是不触发的任何前辈元素上的事件处理函数
xxxxxxxxxx
<!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>
<script src="./js/jquery-3.6.0.min.js"></script>
<style>
div{
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div>
<button>按钮</button>
</div>
<script>
$("div").click("click",function(e){
console.log("div");
})
$("button").click("click",function(e){
e.stopPropagation();
console.log("button");
})
</script>
</body>
</html>
实时效果反馈
1. 下列关于事件对象,哪个是阻止事件冒泡的方法:
A target()
B currentTarget()
C preventDefault()
D stopPropagation()
答案
1=>D