当前位置:网站首页>Description of event flow
Description of event flow
2022-06-30 09:56:00 【zimingyo】
Event flow and two-stage description
Flow of events : It refers to the flow path during the complete execution of the event
Two phases : Capture phase ( Father to son ) And bubbling stage ( Son to father )
Event capture and event bubbling
Event Bubbling
- Concept : When an element's event is triggered , The same event will be triggered in turn in all the ancestor elements of the element .
- Simple understanding : When an element triggers an event , All parent elements will be called up in turn Events of the same name ( It means that the trigger event type is the same : A fellow click)
- Event bubbling exists by default
Event capture - Concept : from DOM The root element of starts to execute the corresponding event ( From the outside to the inside )
- Event capture needs to write corresponding code to see the effect
- Code
DOM.addEventListener( Event type , Event handler , Whether to use capture mechanism ) - explain :
addEventListener The third parameter is passed in true The capture phase triggers when representing ( Rarely used )
If the incoming false Indicates that the bubbling phase triggers , The default is false
If used L0 Event monitoring , Then there is only the bubbling stage , No capture
Stop the flow of events
- Because there is a bubble mode by default , Therefore, it is easy for events to affect parent elements
- If you want to limit the event to the current element , You need to stop the flow of events
- To stop the flow of events, you need to get the event object
- grammar :
Event object .stopPropagation() - This method can block the flow and propagation of events , Not just in the bubbling phase , The capture phase is also effective .
Mouse over event :
- mouseover and mouseout There will be bubbling effect
- mouseenter and mouseleave No bubbling effect
Blocking default behavior : For example, clicking a link does not jump , Form field cannot be submitted
grammar :e.preventDefault()
The difference between the two registration Events
Tradition on register (L0)
bt.οnclick=function(){}
- Same object , The events registered later will overwrite the events registered earlier
<body>
<button> Click on </button>
<script> let bt=document.queryselect('button') bt.onclick=function(){
alter(11) } bt.onclick=function(){
alter(12) } // The execution result is 12 </script>
</body>
- Use it directly null The event can be unbound by overwriting
bt.οnclick=null
- It's all performed in the bubbling phase
Event listening registration (L2)
bt.addEventListener('click',function(){})
bt.addEventListener(‘click’, Function name )
function Function name (){}
- grammar :addEventListener( Event type , Event handler , Whether to use capture ) [true It's capture ,false It's bubbling ( Default )]
- The events registered later will not overwrite the events registered earlier ( The same thing )
- The third parameter can be used to determine whether it is in the bubble or capture phase
- You have to use removeEventListener( Event type , Event handler , Whether to use capture )
bt.addEventListener('click',add)
function add(){
alter(35)
}
bt.removeEventListener('click',add)
// Can't get 35
- Anonymous functions cannot be unbound ( For the second form )
边栏推荐
猜你喜欢

Difference between bow and cbow

目标检测yolov5开源项目调试

Rider打开Unity脚本后没有提示

Flume learning II - Cases

Hospital integration platform super fusion infrastructure transformation scheme

浏览器复制的网址粘贴到文档是超链接

JVM notes (III): analysis of JVM object creation and memory allocation mechanism

LVS load balancing

Shenhe thermomagnetic: Super fusion dual active cluster solution for MES system

Valuenotifier and valuelistenablebuilder in fluent
随机推荐
MySQL index optimization miscellaneous
Work notes: SendTo failed errno 22
What makes flutter special
MySQL internal component structure
[ubuntu-mysql 8 installation and master-slave replication]
【新书推荐】Deno Web Development
Notes on masking and padding in tensorflow keras
布隆过滤器
Read the difference and connection between hyperfusion and private cloud
安装和使用
Appium自动化测试基础 — adb shell 命令
Follow the wechat oauth2.0 access scheme
Installing Oracle database process in windows2007 on VM
Techtarget: Interpretation of the basic concept of super fusion cloud
Quick completion guide for manipulator (4): reducer of key components of manipulator
单片机 MCU 固件打包脚本软件
Eight sorts (I)
将小程序容器技术应用到物联网IoT生态建设中
[new book recommendation] mongodb performance tuning
Difference between bow and cbow