简体   繁体   English

使用 querySelectorAll 在几个图标上添加事件侦听器?

[英]add an event listener on couple of icons using querySelectorAll?

I have this problem I want to add a on click event on some icons in UI after the click happens a forEach is triggered how to get a specific icon using querySelectorAll.我有这个问题我想在点击发生后在 UI 中的某些图标上添加一个点击事件触发 forEach 如何使用 querySelectorAll 获取特定图标。

 function someFunction() { let [...icon] = document.querySelectorAll('.fas'); icon.addEventListener('click', (e) => { let items = icon.closest('.father').querySelectorAll('.item'); items.forEach((item) => (item.style.display = 'none')); }) } someFunction()
 <div class"father"> <div class="fas">-</div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> </div> <div class"father"> <div class="fas">-</div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> </div>

What your probably after is something called delegated events.您可能追求的是所谓的委托事件。

Instead of attaching the event to each .fas you attach further up the tree, often is a good idea to also use another container to capture these in, below I created another div with class items, you could attach to the document but the less an event has to bubble the better.不是将事件附加到您附加到树上的每个.fas ,通常还使用另一个容器来捕获这些事件是个好主意,下面我创建了另一个带有类项目的 div,您可以附加到文档,但越少事件要冒泡就好了。

ps.附: also a slight bug in your html <div class"father"> you was missing the = .. <div class="father">您的 html <div class"father">还有一个小错误,您错过了= .. <div class="father">

 document.querySelector('.items').addEventListener('click', (e) => { if (e.target.classList.contains('fas')) { for (const i of e.target.closest('.father').querySelectorAll('.item')) i.style.display = 'none'; } });
 <div class="items"> <div class="father"> <div class="fas">-</div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> </div> <div class="father"> <div class="fas">-</div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> <div class="item"><p>Text</p></div> </div> </div>

  1. You don't need to destructure icons .您不需要解构icons You can use forEach on the node list without problems.您可以毫无问题地在节点列表上使用forEach

  2. Your HTML was missing some = signs with the father class.您的 HTML 缺少father类的一些=符号。

  3. querySelectorAll('.item') not querySelectorAll('.items') . querySelectorAll('.item')不是querySelectorAll('.items')

 function someFunction() { let icons = document.querySelectorAll('.fas'); icons.forEach(icon => { icon.addEventListener('click', (e) => { let items = e.target.closest('.father').querySelectorAll('.item'); items.forEach(item => item.style.display = 'none'); }); }) } someFunction();
 <div class="father"> <div class="fas">-</div> <div class="item"> <p>Text</p> </div> <div class="item"> <p>Text</p> </div> <div class="item"> <p>Text</p> </div> </div> <div class="father"> <div class="fas">-</div> <div class="item"> <p>Text</p> </div> <div class="item"> <p>Text</p> </div> <div class="item"> <p>Text</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM