gpt4 book ai didi

javascript - js中两个相同监听器的捕获和冒泡的执行顺序

转载 作者:行者123 更新时间:2023-11-28 02:31:09 25 4
gpt4 key购买 nike

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>capture and bubble</title>
<style type="text/css">
#child{
background: red;
width:50px;
height:50px;
}
#father{
width:100px;
height:100px;
background:green;
}
</style>
</head>
<body>
<div id='father'>
<div id='child'></div>
</div>
</body>
<script type="text/javascript">
var parent = document.getElementById("father");
var child = document.getElementById('child');
var html = document.getElementsByTagName("html")[0];
var body = document.body;
parent.addEventListener("click",function() {
console.log("I am capturing parent");
},true);
parent.addEventListener("click",function() {
console.log("I am parent");
},false);
child.addEventListener("click",function() {
console.log("I am capturing child");
},true);
child.addEventListener("click",function() {
console.log("I am child");
},false);
body.addEventListener("click",function() {
console.log("I am capturing body");
},true);
body.addEventListener("click",function() {
console.log("I am body");
},false);
html.addEventListener("click",function() {
console.log("I am capturing html");
},true);
html.addEventListener("click",function() {
console.log("I am html");
},false);
parent.addEventListener("click",function() {
console.log("I am capturing parent");
},true);
parent.addEventListener("click",function() {
console.log("I am parent");
},false);
</script>
</html>

上面的html中包含了一个显示capture和bubble执行顺序的js,我为parent(div father)绑定(bind)了两次capture和bubble的监听器,为parent(div father)绑定(bind)了一对capture和bubble ) 是js脚本部分的第5行到第10行; parent(div father) 的另一对 capture 和 bubble 在 js 脚本部分的末尾。

点击绿色div(父div),在控制台中获取结果。

test.html:50 I am capturing html
test.html:44 I am capturing body
test.html:32 I am capturing parent
test.html:35 I am parent
test.html:56 I am capturing parent
test.html:59 I am parent
test.html:47 I am body
test.html:53 I am html

为什么结果不是下面的?

test.html:50 I am capturing html
test.html:44 I am capturing body
test.html:32 I am capturing parent
test.html:56 I am capturing parent
test.html:35 I am parent
test.html:59 I am parent
test.html:47 I am body
test.html:53 I am html

请详细说明。

最佳答案

MDN documentation for addEventListener状态:

Note: For event listeners attached to the event target, the event is in the target phase, rather than the capturing and bubbling phases. Events in the target phase will trigger all listeners on an element in the order they were registered, regardless of the useCapture parameter.

这就是这里发生的事情。事件监听器附加到事件目标(在您的示例中为 parent),因此事件处于目标阶段并且监听器按照它们注册的顺序触发。

请注意,如果您单击 child,这不会发生,因为该事件处于捕获或冒泡阶段。预期的结果是:

I am capturing html
I am capturing body
I am capturing parent
I am capturing parent
I am capturing child
I am child
I am parent
I am parent
I am body
I am html

事件流有很好的解释和图表here .

enter image description here

关于javascript - js中两个相同监听器的捕获和冒泡的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50682584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com