当前位置:网站首页>JS events (add, delete) and delegates
JS events (add, delete) and delegates
2022-07-06 02:31:00 【MyDreamingCode】
1. The traditional way
var btn1 = document.querySelector('#btn1');
btn1.onclick = function() { // Registration events
alert('hello');
this.onclick = null; // Delete event
}
2. addEventListener (IE9 above )
btn1.addEventListener('click', fn);
function fn() {
alert('hello');
btn1.removeEventListener('click', fn);
}
3. attachEvent(IE6、7、8)
btn1.attachEvent('onclick', fn);
function fn() {
alert('hello');
btn1.detachEvent('onclick', fn);
}
4. DOM Flow of events : Capture phase -> Current target stage (target)-> bubbling phase
Event delegation : Add to parent node event Monitor , Then use the... Of the event object target To find the current click ( Or any other act ) Bubbling of nodes to trigger events .
Be careful :this Refers to the bound event
e.target It refers to who triggered this event ( For example, who did you click on )
For example, the following cases :ul registered click event , be this Point to ul, But if the mouse clicks li Elements , be target by li.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li> Click on , Pop up </li>
<li> Click on , Pop up </li>
<li> Click on , Pop up </li>
<li> Click on , Pop up </li>
<li> Click on , Pop up </li>
</ul>
<script>
var ul = document.querySelector('ul');
ul.addEventListener('click', function(e) {
alert(123);
console.log(e.target);
})
</script>
</body>
</html>
attach :1. Stop bubbling :event.stopPropagation()
2. Block default events :event.preventDefault()
<body>
<p>hahahhah</p>
<script>
var p = document.querySelector('p');
//contextmenu: Right mouse button menu
p.addEventListener('contextmenu',function(e) {
e.preventDefault();
})
//selectstart: Mouse starts to select
p.addEventListener('selectstart',function(e) {
e.preventDefault();
})
</script>
</body>
3. Enter the text box and zoom in
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.wrap {
position: relative;
width: 200px;
margin: 100px auto;
}
.tip {
width: 200px;
height: 30px;
font-size: 18px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
}
input {
position: absolute;
top: 30px;
width: 200px;
height: 20px;
outline: none;
}
</style>
</head>
<body>
<div class="wrap">
<div class="tip"></div>
<input type="text" placeholder=" Please enter the goods order number ">
</div>
<script>
var tip = document.querySelector('.tip');
var input = document.querySelector('input');
document.addEventListener('keyup', function() {
if(input.value != '') {
tip.innerHTML = input.value;
tip.style.display = 'block';
}else {
tip.style.display = 'none';
}
})
input.addEventListener('blur', function() {
tip.style.display = 'none';
})
input.addEventListener('focus', function() {
if(this.value != '') {
tip.style.display = 'block';
}
})
</script>
</body>
</html>
边栏推荐
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
- Redis installation
- Sword finger offer 29 Print matrix clockwise
- HDU_p1237_简单计算器_stack
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Use image components to slide through photo albums and mobile phone photo album pages
- [robot hand eye calibration] eye in hand
- 2022.02.13
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
Sword finger offer 30 Stack containing min function
Structural theme model (I) STM package workflow
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
Building the prototype of library functions -- refer to the manual of wildfire
Black high-end responsive website dream weaving template (adaptive mobile terminal)
0211 embedded C language learning
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
构建库函数的雏形——参照野火的手册
随机推荐
【coppeliasim】高效传送带
模板_求排列逆序对_基于归并排序
剑指 Offer 29. 顺时针打印矩阵
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
I changed the driver to 5.1.35, but it is still the same error. I can succeed even now, but I will report this every time I do an SQL operation
MySQL (IV) - transactions
A doctor's 22 years in Huawei
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
[robot library] awesome robots Libraries
有沒有sqlcdc監控多張錶 再關聯後 sink到另外一張錶的案例啊?全部在 mysql中操作
Template_ Find the reverse pair of permutations_ Sort based on merge
Use image components to slide through photo albums and mobile phone photo album pages
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
2022 edition illustrated network pdf
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
ftp上传文件时出现 550 Permission denied,不是用户权限问题
数据准备工作
Bigder: I felt good about the 34/100 interview, but I didn't receive the admission
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition