当前位置:网站首页>Event object, do you know it well?
Event object, do you know it well?
2022-08-02 10:13:00 【Small cheng classmates】
Remember the last time you used the event object?
I guess it must be used to get the target or to prevent the event from bubbling.
event.stopPropagation()copy codeAfter reading some code and articles recently, I found that the Event object is not as simple as imagined, of course, it is still very simple, and it has more rich uses!
Foreword
The Event object is the event object, which is generated after the event occurs and passed to the listener function as a parameter.It is a constructor natively supported by browsers, and all events are instances of this object
const event = new Event(type, options)copy codeThe Event constructor accepts two parameters, the first parameter is the name of the event, and the second parameter is a configuration object with three main properties
- bubbles: whether to bubble
- cancelable, whether the event can be cancelled, that is, can the event be cancelled by
preventDefault - composed, does the listener fire outside the DOM root node
The default values of the above properties are all false
Next, an event is created through the constructor method. We can use dispatchEvent to trigger this event
const event = new Event('watch',{'bubbles': true,'cancelable': false})document.dispatchEvent(event)copy codeInstance attributes
Event.eventPhase
TheEvent.eventPhase property returns an integer constant representing the current phase of the event
const phase = event.eventPhasecopy codeThe return value has 4 states
- 0 event did not occur
- 1 Event is in capture phase,
- 2 event reaches target node
- 3 The event is in the bubbling phase
Event.cancelable & Event.cancelBubble
Event.cancelable Returns a boolean value, indicating whether the event can be canceled.
Most events can be canceled, but the events generated by the Event constructor cannot be canceled by default.
const evt = new Event('foo');evt.cancelable // falsecopy codeIt is worth mentioning that when cancelable returns false, using event.preventDefault() will have no effectEvent.cancelBubble also returns whether it is currently blockedBubbling, set to true is the same as executing stopPropagation
Event.currentTarget & Event.target
After the event occurs, it will go through two stages of capture and bubbling, and pass through multiple DOM nodes in turn.
- event.target indicates the node that triggered the event initially, and will not change as the event propagates
- event.currentTarget indicates the node that the event is currently passing through, that is, the node where the current listener function is located, and the value will change
In the process of event propagation, the values of the Event.target and Event.currentTarget properties inside the listener functions of different nodes are different.
Event.type
Returns the type of event, specified when the event is generated, read-only property
const event = new Event('ljc')event.type // 'ljc'copy codeEvent.timeStamp
Returns the timestamp of the event, relative to when the page was loaded successfully
const evt = new Event('ljc');evt.timeStamp // 111.11copy codeThe precision of the returned value depends on the settings of the browser
We can use this property to calculate the speed of the mouse movement, how many pixels per second
Event.detail
This property is only available for browser events and returns a value.It depends on the type of event, such as
- Click event, detail returns the number of times the mouse is pressed (1 means single click, 2 means double click, 3 means triple click)
- The scroll wheel event, detail returns the distance of the scroll wheel in the positive direction, or the distance in the negative direction, the return value is a multiple of 3
Event.isTrusted
Indicates whether the current event is generated by real user behavior. For example, if the click event is generated by the user, it will return true
The event generated by the Event constructor will return false
Instance methods
preventDefault & stopPropagation
Prevent default events
Prevent bubbling
stopImmediatePrapagation
TheEvent.stopImmediatePropagation method prevents other listener functions for the same event from being called, regardless of whether the listener function is defined on the current node or another node.That is, this method stops the propagation of the event more thoroughly than Event.stopPropagation() .
If the same node specifies multiple listener functions for the same event, these functions will be called sequentially according to the order in which they were added.As long as one of the listener functions calls the Event.stopImmediatePropagation method, the other listener functions will not be executed.
function l1(e){e.stopImmediatePropagation();}function l2(e){console.log('hello world');}el.addEventListener('click', l1, false);el.addEventListener('click', l2, false);copy codeThe above code is on the el node, adding two listener functions l1 and l2 for the click event.Since l1 calls the event.stopImmediatePropagation method, l2 will not be called.
composedPath
Returns an array containing the deepest node of the event and all the upper nodes that bubbled through in turn
const div = document.querySelector('div')div.addEventListener('click', (e) => {console.log(e.composedPath())})// [a, div, body, html, document, window]copy codeThe deepest point of the click node is the a node, and the top layer is the window, so the path is from a to window
The above is the whole content of this article, I hope it will give you a better understanding of the Event object!
边栏推荐
- 只问耕耘,不问收获,其实收获却在耕耘中
- 如何选择一块真正“好用的、性能高”的远程控制软件
- QT专题:自定义部件
- The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes
- 未知内容监控
- 向量点积(Dot Product),向量叉积(Cross Product)
- iNFTnews | 看见元宇宙的两面,何谓全真互联网和价值互联网?
- Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
- Application scenarios of js anti-shake function and function throttling
- Linux系统卸载,安装,升级,迁移clickHouse数据库
猜你喜欢

你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑

Do you agree with this view?Most businesses are digitizing just to ease anxiety

Rear tube implements breadcrumb function

读博一年后对机器学习工程的思考

yolov7创新点

食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了

Verilog's random number system task----$random

Two-dimensional array piecemeal knowledge sorting

如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?

Facebook自动化数据分析方案,广告投放省心省力
随机推荐
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
瑞萨RZ/G2L处理器详细测评
matlab-day02
Verilog's random number system task----$random
阿里CTO程立:阿里巴巴开源的历程、理念和实践
软件测试X模型
斯皮尔曼相关系数
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
软件工程国考总结——选择题
LayaBox---TypeScript---三斜线指令
后管实现面包屑功能
Facebook's automated data analysis solution saves worry and effort in advertising
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
wireshark的安装教程(暖气片安装方法图解)
Event 对象,你很了解吗?
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
Application scenarios of js anti-shake function and function throttling
Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
8月份的.NET Conf 活动 专注于 .NET MAUI