简体   繁体   English

循环 ES6 中的 removeListener

[英]removeListener inside for loop ES6

I'm having trouble removing a mouseover/mouseout listener inside a foor loop.我在删除 foo 循环内的鼠标悬停/鼠标悬停侦听器时遇到问题。 Let's say I have a menu that on desktop and on hover shows a dropdown, but to open the dropdown I want it on click on mobile.假设我有一个在桌面和悬停时显示下拉菜单的菜单,但要打开下拉菜单,我希望它在移动设备上单击。 I need to remove the hover on mobile because I don't want it to be doing both.我需要删除移动设备上的悬停,因为我不希望它同时进行。 Here's my main function that opens the dropdown:这是我打开下拉列表的主要功能:

 addChildrenActive(i){  
        if(this.parent[i].classList.contains('b--nav-a__list-group__list-item--is-active')){
            //removes class to hide the dropdown         
        } else {
            //adds class to show the dropdown
        }
    }
  • note that this function is receiving i as a param.请注意,此函数接收 i 作为参数。

on the other hand, I have the function responsible for the listeners:另一方面,我有负责听众的功能:

 navbarInteraction(){
        if(this.isMobile){ //calculated via another function based on breakpoints that returns true
            for (let i = 0; i < this.parent.length; i++) {
                this.parent[i].addEventListener('click', e => {
                    e.preventDefault();
                    this.addChildrenActive(i)
                });
                this.parent[i].removeEventListener('mouseover', this.addChildrenActive(i));
                this.parent[i].removeEventListener('mouseout', this.addChildrenActive(i));
                
            }
        } else {
            for (let i = 0; i < this.parent.length; i++) {
                this.parent[i].addEventListener('mouseover', e => {
                    this.addChildrenActive(i)
                });
                 this.parent[i].addEventListener('mouseout', e => {
                    this.addChildrenActive(i)
                });
            }
        }
  }

The removeEventListeners when isMobile is not woking for me and I'm currently able to hover and click to show the dropdown.当 isMobile 没有为我唤醒时的 removeEventListeners 并且我目前能够悬停并单击以显示下拉列表。 How should I solve this?我应该如何解决这个问题?

Thanks in advance!提前致谢!

As @Teemu indicated you have to use a named function because you need a reference to that function if you want to remove it from a listener.正如@Teemu 指出的那样,您必须使用命名函数,因为如果您想从侦听器中删除它,则需要对该函数的引用。

So instead of writing an anonymous function inside the addEventListener() you supply it the name of a function you defined elsewhere - and most importantly not inside your loop.因此,与其在addEventListener()中编写匿名函数,不如向它提供您在其他地方定义的函数的名称——最重要的是不在循环内部。

For example:例如:

parent[i].addEventListener('mouseover', e => {
    this.addChildrenActive(i)
});

becomes变成

parent[i].addEventListener('mouseover', mouseOverHandler);

and it's named function它被命名为函数

function mouseOverHandler(e)
{
  console.log("mouse over", e.currentTarget);
}

You might have noticed the e parameter inside the function.您可能已经注意到函数内部的e参数。 This will be populated with data related to the event eg the name of the event or which element caused it.这将填充与事件相关的数据,例如事件的名称或导致它的元素。

This listener can then be removed using:然后可以使用以下方法删除此侦听器:

parent[i].removeEventListener('mouseover', mouseOverHandler);

Let me give you a more complete example:让我给你一个更完整的例子:

 class Navigation { constructor() { this.isMobile = false; this.parent = document.querySelectorAll("div"); this.navbarInteraction(); } navbarInteraction() { for (let i = 0; i < this.parent.length; i++) { this.parent[i].removeEventListener('mouseover', this.addChildrenActive); this.parent[i].removeEventListener('click', this.addChildrenActive); if (this.isMobile) { this.parent[i].addEventListener('click', this.addChildrenActive); } else { this.parent[i].addEventListener('mouseover', this.addChildrenActive); } } } addChildrenActive(e) { console.log("addChildrenActive()", e.currentTarget, e.type); } } let navigation = new Navigation();
 <div id="divA">DIV1</div> <div id="divB">DIV2</div> <input type="checkbox" onchange="navigation.isMobile=this.checked;navigation.navbarInteraction();"> <span>isMobile</span>

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

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