javascript eventlistener中true和false之间的区别 [英] difference between true and false in javascript eventlistener

查看:83
本文介绍了javascript eventlistener中true和false之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对事件监听器的概念有疑问.下面的两个代码有什么区别我对真/假部分存有疑问.当我将练习代码中的第一代码替换为第二代码时,不会发生任何变化.

i have a doubt in eventlistener concept.What is the difference between bellow two codes i have doubt in the true/false section.no change happens when i replace the 1st code with the second code in my practice code.

a.addEventListener("click", modifyText, true);
a.addEventListener("click", modifyText, false);

推荐答案

addEventListener 中的true和false是布尔值,用于指定是否需要捕获事件.

true and false in addEventListener is Boolean that specifies whether the event needs to be captured or not.

这是语法和详细信息:

object.addEventListener (eventName, function, useCapture);

eventName::字符串,用于指定要侦听的事件的名称.此参数区分大小写!

eventName: String that specifies the name of the event to listen for. This parameter is case sensitive!

函数:表示事件发生时要调用的事件侦听器函数.发生事件时,将初始化事件对象并将其作为第一个参数传递给事件处理程序.事件对象的类型取决于当前事件.

function: Represents the event listener function to be called when the event occurs. When an event occurs, an event object is initialized and passed to the event handler as the first parameter. The type of the event object depends on the current event.

useCapture:布尔值,指定是否需要捕获事件.以下值之一:

useCapture: Boolean that specifies whether the event needs to be captured or not. One of the following values:

false -> Register the event handler for the bubbling phase. 
true -> Register the event handler for the capturing phase.

冒泡和捕获阶段:

冒泡:事件对象以相反的顺序从目标的祖先传播,从目标的父对象开始,以defaultView结束.此阶段也称为冒泡阶段.在此阶段注册的事件侦听器在达到目标后必须处理该事件.

bubbling: the event object propagates through the target's ancestors in reverse order, starting with the target's parent and ending with the defaultView. This phase is also known as the bubbling phase. Event listeners registered for this phase must handle the event after it has reached its target.

捕获:事件对象必须通过目标的祖先从defaultView传播到目标的父对象.此阶段也称为捕获阶段.在此阶段注册的事件侦听器必须在事件到达目标之前对其进行处理.

capturing: the event object must propagate through the target's ancestors from the defaultView to the target's parent. This phase is also known as the capturing phase. Event listeners registered for this phase must handle the event before it reaches its target.

有关事件流的更多详细信息,: DOM事件架构

这篇关于javascript eventlistener中true和false之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆