当前位置:网站首页>JS event introduction
JS event introduction
2022-07-29 01:33:00 【godton】
Flow of events
Click on an element in a page , This element is not the only one that triggers , It also includes all the parent content outside this element ; Use the description of the red book : Draw several concentric circles on a piece of paper , Put your finger in the center of the circle , It is also placed in the center of all circles . IE Browser and Netscape browser use two methods to deal with this contradiction .
1. Event Bubbling
IE The browser adopts the bubbling method , Bubbling is from the underlying element To the highest element Document First floor One layer bubbles upward :div -> body -> html -> document
2. Event capture
Netscape browser is just the opposite , From the top , The element finally captures the event document -> html -> body -> div
And in the DOM2 period , Three stages of the event are defined , Event capture 、 Reach the target 、 Event Bubbling .
Event handler
Event handling is divided into DOM0 and DOM2 Event handling ,DOM0 In design pattern, it is observer pattern , Is triggered in the bubbling stage
DOM0 Event handler
let btn = document.getElementById('mybtn')
btn.onClick = function() {
console.log('clicked')
}
but DOM1 The event will be overwritten , Only one corresponding function can be processed .
DOM2 Event handler
DOM2 The event processing of can control whether it is triggered in the bubbling stage or the capture stage , Is the publish subscribe mode .addEventListener( 'Event', Function, Boolean)
Event Is the name of the triggered event ,Function Is the corresponding processing function ,Boolean Of flase Is the default value , Indicates that in the bubbling stage ;true For the capture phase .
let btn = document.getElementById('mybtn')
btn.addEventLisenter('click' _ => {
console.log('clicked'), true)
DOM2 The advantage of is that you can add multiple processing functions , And will not be covered
let btn = document.getElementById('mybtn')
btn.addEventLisenter('click' _ => {
console.log('clicked'), true)
btn.addEventLisenter('click' _ => {
console.log('HelloWorld'), false)
Event object
All events have their own event object , This object will be passed in when calling the function
let btn = document.getElementById('mybtn')
btn.onClick = function(e) {
console.log(e) //PointerEvent {isTrusted: true, pointerId: 1, width: 1, height: 1, pressure: 0, …}
}
let btn = document.getElementById('mybtn')
btn.addEventLisenter('click' e => {
console.log(e), true)
//PointerEvent {isTrusted: true, pointerId: 1, width: 1, height: 1, pressure: 0, …}
Objects contain many properties , Include target,type, view Various operations on the event are based on the event object .
Event type
There are many types of events , There are different events under each event type , Most of the event types are as follows :
- User interface events
- "Event"
- Wheel event
- Input event
- Keyboard events
- Composite event
- html5 event
- ( Mobile devices )
- Touch screen events
- Device events
- Gesture events
边栏推荐
猜你喜欢
![[SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions](/img/db/d0255d7036f7003d380c8888fed88b.png)
[SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions

云原生应用综合练习上

了解各种路径

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益

BOM系列之定时器

如何选择专业、安全、高性能的远程控制软件

第二轮Okaleido Tiger热卖的背后,是背后生态机构战略支持

SQL question brushing: find the last of all employees who have been assigned departments_ Name and first_ Name and Dept_ no

matplotlib中文问题

【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
随机推荐
如何选择专业、安全、高性能的远程控制软件
After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device
数据库的decimal类型的数据,发现可以通过resultSet.getDouble去拿到这个数据,但是通过getObject却拿不到这个属性。
Error installing mysqlclient module on MAC system
uniapp movable-view表格缩放过程想保持容器高度不变的解决办法
File “manage.py“, line 14 ) from exc
第二轮Okaleido Tiger热卖的背后,是背后生态机构战略支持
Three ways of creating indexes in MySQL
ELMO,BERT和GPT简介
BOM系列之window对象
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
Regular checksum time formatting
uniapp createSelectorQuery().select 获取返回null报错
10 major network security incidents in the past 10 years
什么是原码、反码和补码
Django reports an error using pymsql module django.db.utils.operationalerror
LeTax记录\documentclass{},authoryear属性使用
Learning summary of time complexity and space complexity
Behind the second round of okaleido tiger sales is the strategic support of ecological institutions