简体   繁体   English

使用 forEach 将事件侦听器添加到元素数组

[英]Adding event listeners to an array of elements using forEach

I created nine blue div boxes and I'm trying to write a Javascript that will set the opacity of each box to 0 once the box is hovered over.我创建了九个蓝色 div 框,我正在尝试编写一个 Javascript,一旦框悬停,它将把每个框的不透明度设置为 0。 I made an array of all the boxes but when I try to apply addEventListener to each array element using forEach method it returns "Cannot set properties of undefined" error.我制作了一个包含所有框的数组,但是当我尝试使用 forEach 方法将 addEventListener 应用于每个数组元素时,它返回“无法设置未定义的属性”错误。 What mistake am I making?我犯了什么错误?

 const boxes=document.getElementsByClassName("box"); const arrayOfBoxes=Array.from(boxes); arrayOfBoxes.forEach((box)=>{box.addEventListener("mouseover",hide)}); function hide(element){ element.style.opacity=0.0; }
 <div class="boxes"> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> </div>

Did you know you could achieve this using CSS only?您是否知道仅使用 CSS 可以实现此目的?

 .box { opacity: 1; transition: 250ms all; float: left; }.box:hover { opacity: 0; }
 <div class="boxes"> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> </div>

The argument to an event listener is the event, not the element.事件侦听器的参数是事件,而不是元素。 So use event.currentTarget to get the element.所以使用event.currentTarget来获取元素。

function hide(event){
    event.currentTarget.style.opacity=0.0;
}

Instead of adding an event listener to every single div (which can cause problems as the number of divs likely increases), just add one event listener to the parent element that listens for the 'mouseover' event to achieve the same exact goal.无需为每个div添加事件侦听器(这可能会随着 div 数量的增加而导致问题),只需将一个事件侦听器添加到侦听'mouseover'事件的父元素即可实现相同的确切目标。

 const boxes = document.querySelector('div.boxes'); const handleHover = (e) => { if (e.target === boxes) return; e.target.style.opacity = 0; }; boxes.addEventListener('mouseover', handleHover);
 <div class="boxes" style="display: flex; flex-direction: column; gap: 10px"> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> <div class="box" style="background-color: blue; height: 100px; width: 100px"></div> </div>

There's three ways to identify the actual tag a user has interacted with:有三种方法可以识别用户与之交互的实际标签:

//.target property points to the tag the user interacted with
event.target.style.opacity = '0.0';

/*
.currentTarget property points to the tag that the event listener is attached to.
*/
event.currentTarget.style.opacity = '0.0';

/*
"this" a keyword that points to an object. If "this" is in an event handler, it
will point to the same thing .currentTarget points to.
*/
this.style.opacity ="0.0"

Also, the event handler passes the event object by default, so when you passed element , it was considered the event object.此外,事件处理程序默认传递事件 object,因此当您传递element时,它被认为是事件 object。 In order not to confuse anyone that happens to read your code, choose a more accurate moniker like: event , e , evt , etc.为了不让碰巧阅读您的代码的人感到困惑,请选择更准确的名字,例如: eventeevt等。

 const boxes=document.getElementsByClassName("box"); const arrayOfBoxes=Array.from(boxes); arrayOfBoxes.forEach((box)=>{box.addEventListener("mouseover",hide)}); function hide(event) { this.style.opacity=0.0; }
 <div class="boxes"> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> <div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div> </div>

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

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