当前位置:网站首页>JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
2022-07-28 17:28:00 【HandsomeDanielWu】


this




<body>
<button id="btn">按我添加新的li列表</button>
<ul id="list">
<li>默认标签</li>
<li>默认标签</li>
<li>默认标签</li>
</ul>
<script>
var oBtn = document.getElementById('btn');
var oList = document.getElementById('list');
var lis = oList.getElementsByTagName('li');
// 变红色
var redFunc = function(){
this.style.color = 'red';
};
//给默认的li添加点击事件
for (var i = 0; i < lis.length; i++){
lis[i].onclick = redFunc;
};
//添加新标签
oBtn.onclick = function () {
var oli = document.createElement('li');
oli.innerHTML = '新添加选项';
oList.appendChild(oli);
//新添加的标签,必须手动添加监听事件
oli.onclick = redFunc;
};
</script>
</body>

解决上述问题:事件委托


target currentTarget












onmouseenter onmouseover



<li>是块级元素,天生宽度max



边栏推荐
- Kali doesn't have an eth0 network card? What if you don't connect to the Internet
- SQL审核工具自荐Owls
- vim学习手册
- R language text mining and natural language processing tutorial
- Time waits for no man. The rise of TSDB is at the right time
- Leetcode skimming - super power 372 medium
- From Bayesian filter to Kalman filter (I)
- Jestson nano Object detection
- Application of TSDB in civil aircraft industry
- Random finite set RFs self-study notes (6): an example of calculation with the formula of prediction step and update step
猜你喜欢

6-20 vulnerability exploitation proftpd test

R language text mining and natural language processing tutorial

QT widget promoted to QWidget

Lookup - lookup of sequential table and ordered table

ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)

VAE:变分自编码器的理解与实现

Web 3.0 development learning path

6-20漏洞利用-proftpd测试
![[radar] radar signal online sorting based on kernel clustering with matlab code](/img/56/1f8e8690b47fc4a1f101d4e530b87f.png)
[radar] radar signal online sorting based on kernel clustering with matlab code

Application of time series database in bridge monitoring field
随机推荐
As for the white box test, you have to be skillful in these skills~
Pytest custom hook function
QT running image
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
Powerbi time series analysis, prediction and visualization tutorial
Application of time series database in monitoring operation and maintenance platform
Photoshop web design practical tutorial
About ASM redundancy
Youqilin system installation beyondcomare
剑指 Offer II 109. 开密码锁
More loading in applets (i.e. list paging)
Photoshop responsive web design tutorial
Gmoea code operation 2 -- establishment and operation of operation environment
BM16 delete duplicate elements in the ordered linked list -ii
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
QT function optimization: QT 3D gallery
Avoidance Adjusted Climbrate
用LEX(FLEX)生成PL语言的词法分析器
Remember a uniapp experience
Wechat official account custom sharing and updateappmessagesharedata are all successful. Why is it a link that is shared?