简体   繁体   English

对于数组中的每个元素,我希望它有另一个元素,每个元素都有独特的动作

[英]For each element inside an array I want it to have another for each that will have unique action

I wanted to make each element inside myArray will have it's unique action, but I end up with only one of them working.我想让 myArray 中的每个元素都有它独特的动作,但我最终只有一个在工作。
I have tried one more way of doing it that worked, but it was a complete boilerplate and I'm looking for a better solution than that.我已经尝试了另一种可行的方法,但它是一个完整的样板,我正在寻找比这更好的解决方案。
More details: For each element (Another array of buttons) inside myArray it will have unique action like scrollIntoView of some element in HTML.更多详细信息:对于 myArray 中的每个元素(另一个按钮数组),它将具有独特的操作,例如 HTML 中某些元素的 scrollIntoView。
In HTML I have 4 divs that share the same class and it looks like that:在 HTML 我有 4 个 div 共享相同的 class,它看起来像这样:

<div class='firstDiv'>
<button class="teamBtn"></button>
<button class="serviceBtn"></button>
etc..
</div>
<div class='secondDiv'>
<button class="teamBtn"></button>
<button class="serviceBtn"></button>
etc..
</div>
let aboutSection = document.querySelector('.about')
let serviceSection = document.querySelector('.services')
let teamSection = document.querySelector('.team')
let homeBtn = document.querySelectorAll('.homeBtn');
let aboutBtn = document.querySelectorAll('.aboutBtn');
let serviceBtn = document.querySelectorAll('.serviceBtn')
let teamBtn = document.querySelectorAll('.teamBtn')

let myArray = [];

myArray[0] = homeBtn;
myArray[1] = aboutBtn;
myArray[2] = serviceBtn;
myArray[3] = teamBtn;
myArray.forEach(el => {
  addEventListener('click', () => {
    teamBtn.forEach(() => {
     teamSection.scrollIntoView();
    });
    serviceBtn.forEach(() => {
      serviceSection.scrollIntoView();
    });

  })
})

You really want delegation from a container wrapping ALL divs.你真的想要一个包装所有 div 的容器的委托。 Then only one event handler is needed for all buttons那么所有按钮只需要一个事件处理程序

 document.getElementById("nav").addEventListener("click",function(e) { const tgt = e.target.closest("button") if (tgt && (tgt.classList.contains("teamBtn") ||tgt.classList.contains("serviceBtn"))) { document.getElementById(tgt.dataset.id).scrollIntoView(); } })
 section div { height: 500px; background-color: red; border: 1px solid black; }
 <nav id="nav"> <div class="firstDiv"> <button class="teamBtn" data-id="team1">Team 1</button> <button class="serviceBtn" data-id="service1">Service 1</button> </div> <div class="secondDiv"> <button class="teamBtn" data-id="team2">team 2</button> <button class="serviceBtn" data-id="service2">Service 2</button> </div> </nav> <section id="content1"> <div id="team1">Team 1</div> <div id="service1">Service 1</div> </section> <section id="content2"> <div id="team2">Team 2</div> <div id="service2">Service 2</div> </section>

暂无
暂无

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

相关问题 我有一个贯穿数组的 API,我想在每个数组元素上向客户端发送数据 - I have an API that runs through an array and I want to send data to the client on each array element 我有一个JavaScript数组,我想下载数组中的每个图像 - I have a JavaScript array and I want to download each image in the array 我有四个按钮,并希望在单击每个按钮时返回唯一的文本 - I have four buttons and want to return unique text on click of each button 我有六个反应文件,每个文件都导出一个数组,我想将六个文件减少到一个 - I have six react files each exports an array and I want to reduce the six files to one 我有一组不同的 ID,我想从每个 ID 中获取数据。 可能吗? - I have an array of different IDs and I want to fetch data from each IDs . Is it possible? 警告:每个孩子都应该有一个唯一的键 - 在 ReactJS 中传递数组 - Warning: Each child should have a unique key - Passing Array In ReactJS 数组或迭代器中的每个孩子都应该有一个唯一的“key” prop - Each child in an array or iterator should have a unique "key" prop 数组或迭代器中的每个子代在reactjs中都应具有唯一的“键”道具 - Each child in an array or iterator should have a unique “key” prop in reactjs 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具 - Warning :Each child in an array or iterator should have a unique “key” prop React - 数组或迭代器中的每个子节点都应该有一个唯一的“key”prop - React - Each child in an array or iterator should have a unique “key” prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM