当前位置:网站首页>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 )
边栏推荐
- MySQL directory
- Comparison problems encountered in recent study
- 【JVM】G1垃圾回收器簡述
- Self service terminal handwritten Chinese character recognition input method library tjfink introduction
- Stack Title: String decoding
- Differences and relationships among hyper convergence, software defined storage (SDS), distributed storage and server San
- Train an image classifier demo in pytorch [learning notes]
- Task summary in NLP
- 云技能提升好伙伴,亚马逊云师兄今天正式营业
- input限制输入
猜你喜欢
MySQL优化
Machine learning note 9: prediction model optimization (to prevent under fitting and over fitting problems)
Dart development skills
[Ubuntu redis installation]
【新书推荐】Cleaning Data for Effective Data Science
[new book recommendation] DeNO web development
Difference between bow and cbow
How to build an all-in-one database cloud machine that meets the needs of information innovation?
JUL简介
力扣 428. 序列化和反序列化 N 叉树 DFS
随机推荐
Shell script functions
MySQL index and data storage structure foundation
Flume learning III
[new book recommendation] DeNO web development
Shenhe thermomagnetic: Super fusion dual active cluster solution for MES system
JWT expiration processing - single token scheme
Shell script multi loop experiment
Mysql database learning 1
P. Summary of NP, NPC, NP hard and other issues
[Ubuntu redis installation]
【JVM】G1垃圾回收器简述
Based on svelte3 X desktop UI component library svelte UI
近期学习遇到的比较问题
事件委托的使用与说明》
Quick completion guide for manipulator (4): reducer of key components of manipulator
Horrible bug records
MySQL index optimization miscellaneous
Tablet PC based ink handwriting recognition input method
Cloud native database
【JVM】G1垃圾回收器簡述