Home >Web Front-end >JS Tutorial >Understand the event bubbling mechanism in JS in one article
This article talks about event bubbling and gives you an in-depth understanding of the event bubbling mechanism in JS. I hope it will be helpful to you!
In the browser client application platform, it is basically generated It is event-driven, that is, an event occurs, and then corresponding actions are taken.
Browser events represent signals that something has happened. The explanation of the incident is not the focus of this article. Friends who have not yet understood it can learn more on Baidu, which will help to better understand the following content.
#What is bubbling?
You should understand the picture below. The bubbles start from the bottom of the water and rise up, from deep to shallow, to the top. On the way up, the bubbles pass through different depth levels of water.
The entire DOM tree; events are passed up from the bottom layers of the DOM tree until they are passed to the root node of the DOM.
Simple case analysis
The following is a simple example to illustrate the bubbling principle:Definition An HTML, there are three simple DOM elements: div1, div2, span, div2 contains span, div1 contains div2; and they are all under the body:
<div> <div> <span>This is a span.</span> </div> </div>
Interface prototype As follows:
, when the body captures the event event, print out the time when the event occurred and the node information that triggered the event:
<script> window.onload = function() { document.getElementById("body").addEventListener("click",eventHandler); } function eventHandler(event) { console.log("时间:"+new Date(event.timeStamp)+" 产生事件的节点:" + event.target.id +" 当前节点:"+event.currentTarget.id); } </script>When we click "This is span", div2, div1, and body in sequence, the output The following information:
Analysis of the above results: , or the sub-element div2 of div, and span. When these elements are clicked, a click event will be generated, and the body will capture it, and then call the corresponding event processing function. Just as bubbles in water rise from the bottom up, so do events.
The schematic diagram of event delivery is as follows:
# Generally, there will be some information during the event delivery process. These are Components of an event: Time when the event occurred Where the event occurred Type of event Current handler of the event Other information ,
The complete html code is as follows:
nbsp;html> <script></script>Insert title here <script> window.onload = function() { document.getElementById("body").addEventListener("click",eventHandler); } function eventHandler(event) { console.log("时间:"+new Date(event.timeStamp)+" 产生事件的节点:" + event.target.id +" 当前节点:"+event.currentTarget.id); } </script> <div> <div> <span>This is a span.</span> </div> </div>
b. Bubbling of termination event
We now want to implement such a function, When div1 is clicked, "Hello, I am the outermost div." pops up. When div2 is clicked, "Hello, I am the second layer div" pops up; when span is clicked, "Hello, I am the second div" pops up. span." From this we will have the following javascript fragment: 预期上述代码会单击span 的时候,会出来一个弹出框 "您好,我是span。" 是的,确实弹出了这样的对话框: 然而,不仅仅会产生这个对话框,当点击确定后,会依次弹出下列对话框: 这显然不是我们想要的! 我们希望的是点谁显示谁的信息而已。为什么会出现上述的情况呢? 原因就在于事件的冒泡,点击span的时候,span 会把产生的事件往上冒泡,作为父节点的div2 和 祖父节点的div1也会收到此事件,于是会做出事件响应,执行响应函数。现在问题是发现了,但是怎么解决呢? 方法一:我们来考虑一个形象一点的情况:水中的一个气泡正在从底部往上冒,而你现在在水中,不想让这个气泡往上冒,怎么办呢?——把它扎破!没了气泡,自然不会往上冒了。类似地,对某一个节点而言,如果不想它现在处理的事件继续往上冒泡的话,我们可以终止冒泡: 在相应的处理函数内,加入 event.stopPropagation() ,终止事件的广播分发,这样事件停留在本节点,不会再往外传播了。修改上述的script片段: 经过这样一段代码,点击不同元素会有不同的提示,不会出现弹出多个框的情况了。 方法二:事件包含最初触发事件的节点引用 和 当前处理事件节点的引用,那如果节点只处理自己触发的事件即可,不是自己产生的事件不处理。event.target 引用了产生此event对象的dom 节点,而event.currrentTarget 则引用了当前处理节点,我们可以通过这 两个target 是否相等。 比如span 点击事件,产生一个event 事件对象,event.target 指向了span元素,span处理此事件时,event.currentTarget 指向的也是span元素,这时判断两者相等,则执行相应的处理函数。而事件传递给 div2 的时候,event.currentTarget变成 div2,这时候判断二者不相等,即事件不是div2 本身产生的,就不作响应处理逻辑。 比较: 从事件传递上看: 方法一在于取消事件冒泡,即当某些节点取消冒泡后,事件不会再传递; 方法二在于不阻止冒泡,过滤需要处理的事件,事件处理后还会继续传递; 优缺点: 方法一缺点:为了实现点击特定的元素显示对应的信息,方法一要求每个元素的子元素也必须终止事件的冒泡传递,即跟别的元素功能上强关联,这样的方法会很脆弱。比如,如果span 元素的处理函数没有执行冒泡终止,则事件会传到p2 上,这样会造成p2 的提示信息; 方法二缺点:方法二为每一个元素都增加了事件监听处理函数,事件的处理逻辑都很相似,即都有判断 if(event.target == event.currentTarget),这样存在了很大的代码冗余,现在是三个元素还好,当有10几个,上百个又该怎么办呢? 还有就是为每一个元素都有处理函数,在一定程度上增加逻辑和代码的复杂度。 我们再来分析一下方法二:方法二的原理是 元素收到事件后,判断事件是否符合要求,然后做相应的处理,然后事件继续冒泡往上传递; 既然事件是冒泡传递的,那可不可以让某个父节点统一处理事件,通过判断事件的发生地(即事件产生的节点),然后做出相应的处理呢?答案是可以的,下面通过给body 元素添加事件监听,然后通过判断event.target 然后对不同的target产生不同的行为。 将方法二的代码重构一下:
结果会是点击不同的元素,只弹出相符合的提示,不会有多余的提示。 p1 delegates its response logic to the body and lets it complete the corresponding logic. It does not implement the corresponding logic. This mode is the so-called event delegation. [Related recommendations: <script>
window.onload = function() {
document.getElementById("box1").addEventListener("click",function(event){
alert("您好,我是最外层div。");
});
document.getElementById("box2").addEventListener("click",function(event){
alert("您好,我是第二层div。");
});
document.getElementById("span").addEventListener("click",function(event){
alert("您好,我是span。");
});
}
</script>
<script>
window.onload = function() {
document.getElementById("box1").addEventListener("click",function(event){
alert("您好,我是最外层div。");
event.stopPropagation();
});
document.getElementById("box2").addEventListener("click",function(event){
alert("您好,我是第二层div。");
event.stopPropagation();
});
document.getElementById("span").addEventListener("click",function(event){
alert("您好,我是span。");
event.stopPropagation();
});
}
</script>
<script>
window.onload = function() {
document.getElementById("box1").addEventListener("click",function(event){
if(event.target == event.currentTarget)
{
alert("您好,我是最外层div。");
}
});
document.getElementById("box2").addEventListener("click",function(event){
if(event.target == event.currentTarget)
{
alert("您好,我是第二层div。");
}
});
document.getElementById("span").addEventListener("click",function(event){
if(event.target == event.currentTarget)
{
alert("您好,我是span。");
}
});
}
</script>
<script>
window.onload = function() {
document.getElementById("body").addEventListener("click",eventPerformed);
}
function eventPerformed(event) {
var target = event.target;
switch (target.id) {
case "span":
alert("您好,我是span。");
break;
case "div1":
alert("您好,我是第二层div。");
break;
case "div2":
alert("您好,我是最外层div。");
break;
}
}
</script>
The following is a schematic diagram:
The above is the detailed content of Understand the event bubbling mechanism in JS in one article. For more information, please follow other related articles on the PHP Chinese website!