当前位置:网站首页>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>
边栏推荐
- RDD creation method of spark
- 在线怎么生成富文本
- SQL table name is passed as a parameter
- UE4 - how to make a simple TPS role (I) - create a basic role
- Zero basic self-study STM32 wildfire review of GPIO use absolute address to operate GPIO
- 在GBase 8c数据库中使用自带工具检查健康状态时,需要注意什么?
- Multi function event recorder of the 5th National Games of the Blue Bridge Cup
- Bigder:34/100 面试感觉挺好的,没有收到录取
- Paper notes: graph neural network gat
- [robot library] awesome robots Libraries
猜你喜欢
Overview of spark RDD
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
Use image components to slide through photo albums and mobile phone photo album pages
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
Adapter-a technology of adaptive pre training continuous learning
RDD conversion operator of spark
从顶会论文看2022年推荐系统序列建模的趋势
Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
随机推荐
Number conclusion LC skimming review - 1
力扣今日题-729. 我的日程安排表 I
Sword finger offer 29 Print matrix clockwise
Shell脚本更新存储过程到数据库
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
一位博士在华为的22年
构建库函数的雏形——参照野火的手册
好用的 JS 脚本
论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
Minecraft 1.18.1、1.18.2模组开发 22.狙击枪(Sniper Rifle)
Use image components to slide through photo albums and mobile phone photo album pages
VIM usage guide
Global and Chinese markets of general purpose centrifuges 2022-2028: Research Report on technology, participants, trends, market size and share
继承的构造函数
高数_向量代数_单位向量_向量与坐标轴的夹角
Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL winter vacation self-study 2022 11 (6)
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]