当前位置:网站首页>Event listening and deleting events - event object - default event - cancel bubbling event - event delegation - default trigger
Event listening and deleting events - event object - default event - cancel bubbling event - event delegation - default trigger
2022-07-23 09:19:00 【Brave * Niuniu】
Event listening and deleting events
DOM0 level :
- The same event of a tag can only be bound to one event function
- Events can be added directly in the line
- All major browsers support , There is no compatibility issue
DOM2 level
- The same event of a tag can be bound to multiple event functions
event object
| function | function |
|---|---|
| pageX | The horizontal position of the cursor relative to the web page |
| pageY | The vertical position of the cursor relative to the web page |
| screenX | The horizontal position of the cursor relative to the screen |
| screenY | The vertical position of the cursor relative to the screen |
| clientX | The horizontal position of the cursor relative to the web page ( Current visible area ) |
| clientY | The vertical position of the cursor relative to the web page ( Current visible area ) |
Default event
| Range | Method |
|---|---|
| DOM in : | event.preventDefault() Blocking default behavior |
| IE in : | event.returnValue=false Return value =false |
Cancel bubbling
| project | Value |
|---|---|
| In standard browser DOM in : | event.stopPropagation() Stop spreading |
| IE8 And the following : | event.cancelBubble=true Cancel the bubbling |
Now it's usually :e.stopImmediatePropagation();// Prevent the execution of subsequent functions of the same event type

var div1 =document.querySelector('.div1')
var div2 =document.querySelector('.div2')
var div3 =document.querySelector('.div3')
div1.addEventListener('click',clickHandler1)
div2.addEventListener('click',clickHandler2)
div3.addEventListener('click',clickHandler3,{
once:true,capture:true})
function clickHandler1(e){
e.stopPropagation();// To prevent a bubble
console.log('div1');
}
function clickHandler2(e){
e.stopPropagation();// To prevent a bubble
console.log('div2');
// e.stopImmediatePropagation();// Prevent the execution of subsequent functions of the same event type
}
function clickHandler3(e){
e.stopPropagation();
console.log('div3');
}
Event delegation
- Delegate the listening event of the child element to the parent element for listening , such , Events triggered by all child elements
- Will pass bubbles to the parent element , Reduced memory overhead
So how to confirm which one is clicked to trigger the event :
e.target e.srcElement(IE8) Finally, click on the target element e.currentTarget this Object elements that listen for events
console.log("div1",e.target,e.currentTarget,e.srcElement,this);
Default trigger
| Event name | function |
|---|---|
| change event | Mainly for forms and form elements , The form element trigger event will bubble to form After modification, out of focus trigger |
<form action="">
<input type="text">
<input type="checkbox">
<textarea name="" id="" cols="30" rows="10"></textarea>
<button type="reset"> Reset </button>
</form>
<div></div>
<script>
var input=document.querySelector("[type=text]");
var checkbox=document.querySelector("[type=checkbox]");
var form =document.querySelector("form");
input.addEventListener("change",changeHandler);
checkbox.addEventListener("change",changeHandler);
form.addEventListener("change",changeHandler);
function changeHandler(e){
console.log(e)
}
</script>
| Event name | function |
|---|---|
| submit | Submit Form |
| reset | Reset form |
| above | All aimed at form Events of the form |
| e.preventDefault(); | Prevent default Events ( Reset / Submit ) |
var form=document.querySelector("form");
// form.addEventListener("submit",submitHandler);
form.addEventListener("reset",submitHandler);
function submitHandler(e){
// e.preventDefault();
console.log(e);
}
| Event name | function |
|---|---|
| resize event | Mainly aimed at window, instead of textarea |
| Trigger | An event is triggered when the window is zoomed |
function resizeHandler(e){
// console.log(e)
// Screen width screen.width
// Window width outerWidth
// Window width / Screen width *100( original 100% Width fontSize Size )
// The window shrinks , The proportion will be reduced , Reset the text size according to the scale
document.documentElement.style.fontSize=outerWidth/screen.width*100+"px"
}
| Event name | function |
|---|---|
| select | Selected events |
| Trigger | Triggered when the text content entered in the text box is selected |
var input=document.querySelector("[type=text]");
input.addEventListener("select",selectHandler);
function selectHandler(e){
console.log(input.selectionStart);// Get the starting subscript of the character in the selected text box
console.log(input.selectionEnd);// Get the ending subscript of the character in the selected text box
}
| Event name | function |
|---|---|
| scroll | The event is triggered when the scroll bar scrolls |
window.addEventListener("scroll",scrollHandler);
function scrollHandler(e){
console.log(document.documentElement.scrollTop)
}
| error error | load load |
|---|---|
| window load DOM The tree is rendered | load Events trigger |
| ajax Communication loading When the communication load is completed | load Events trigger |
| Loading pictures 、js、css、 video , | Trigger after loading |
| When loading fails | Trigger error event |
The incident was distributed
In the event this
边栏推荐
- 1646. Recursive method of getting the maximum value in the generated array
- DOM series prohibit selected text and prohibit right-click menu
- 解析创客教育活动所需的空间实践场
- 35岁程序员,早到的中年危机
- TCP数据传输与性能
- C语言经典练习题(1)——“水仙花数“
- 真人踩过的坑,告诉你避免自动化测试常犯的10个错误
- 一个月学透阿里整理的分布式架构笔记
- SQL用户表的通用设计
- Is it safe to open an account online? How about Galaxy Securities
猜你喜欢

如何高效系统学习 MySQL?

【C语言】文件操作

求解最大公约数和最小公倍数

【面试:并发篇21:多线程:活跃性】死锁、活锁、饥饿
![[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code](/img/df/f29eed667c2a7dc02d93ac3f424198.png)
[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code

砥砺前行新征程,城链科技狂欢庆典在厦门隆重举行

How many of the 50 classic computer network interview questions can you answer? (top)

Swin transformer object detection project installation tutorial

How many of the 50 classic computer network interview questions can you answer? (III)

-bash: wget: 未找到命令
随机推荐
General design of SQL user table
Canal realizes MySQL data synchronization
购买股票开户安全吗,会亏钱吗?
pytorch简单示例汇总
2022.7.22-----leetcode.757
视频点播中相关分辨率说明
【高等数学】矩阵的初等变换和行列式的初等变换
UGUI源码解析——MaskableGraphic
transformer汇总
C语言经典练习题(1)——“水仙花数“
How many points can you get on the latest UnionPay written test for test engineers?
[array]nc77 adjust the array order so that odd numbers precede even numbers (one) - medium
Swin-Transformer-Object-Detection项目安装教程
PyTorch可视化
Summary of some open source libraries that drive MCU hardware debugger (including stlink debugger)
Transformer summary
[ManageEngine] six essential functions of network configuration management
求解最大公约数和最小公倍数
[cloud native] in the era of storm and cloud, the sharp edge of DBAs is out of the sheath
Number theory -- division and blocking, common classic examples.