简体   繁体   English

querySelectorAll 事件侦听器 function 无法正确迭代

[英]querySelectorAll Event Listener function does not iterate correctly

I am trying to create an eventListener() function on a number of map icons.我正在尝试在许多 map 图标上创建一个 eventListener() function。 Each of these map icons have a corresponding span tag with the name of its location.这些 map 图标中的每一个都有一个对应的跨度标签及其位置名称。

The idea here is that when a user hovers over an icon, it will trigger an event listener that will toggle the corresponding span tag.这里的想法是,当用户将鼠标悬停在图标上时,它将触发一个事件侦听器,该事件侦听器将切换相应的 span 标签。 There are ten icons in total.总共有十个图标。 Simple enough on paper.在纸上很简单。

I have managed to iterate using querySelectorAll in that when each icon is hovered over, the event listener fires.我已经设法使用 querySelectorAll 进行迭代,因为当每个图标悬停在上面时,事件侦听器就会触发。 However, my issue here is that the actual toggle only works for the first element in the class list.但是,我的问题是实际切换仅适用于 class 列表中的第一个元素。 So, if we call the first map icon 'A', when I hover over 'B', 'C', 'D' respectively, instead of showing me their span tags, it keeps firing the toggle for 'A' only.因此,如果我们将第一个 map 图标称为“A”,当我 hover 分别在“B”、“C”、“D”上时,它不会向我显示它们的跨度标签,而是只为“A”触发切换。 I think I am getting an iteration wrong here somewhere.我想我在这里某个地方的迭代错误。

HTML: HTML:

<div class="map-icon moreno">
    <span class="map-icon-text">Moreno Valley, CA</span>
</div>

<div class="map-icon dallas">
    <span class="map-icon-text">Dallas, TX</span>
</div>

<div class="map-icon cicero">
    <span class="map-icon-text">Cicero, IL</span>
</div>

<div class="map-icon chattanooga">
    <span class="map-icon-text">Chattanooga, TN</span>
</div>

CSS: CSS:

.map-icon-text {
    transform: scale(0);
    transition: all 0.2s ease-in-out; }

.map-icon-text-active {
    transform: scale(1);
    transition: all 0.2s ease-in-out; }

JS: JS:

const iconText = document.querySelector('.map-icon-text');

function addSpanClass() {
  iconText.classList.add('map-icon-text-active');
}

function removeSpanClass() {
  iconText.classList.remove('map-icon-text-active');
}

const mapIcon = document.querySelectorAll('.map-icon');
for (i = 0; i < mapIcon.length; i++) {
  mapIcon[i].addEventListener('mouseenter', addSpanClass);
  mapIcon[i].addEventListener('mouseleave', removeSpanClass);
}

Ultimately, I am pushing the.map-icon-text-active CSS into the span tag to animate and using the eventListener to toggle this action.最终,我将 .map-icon-text-active CSS 推送到 span 标签中以设置动画并使用 eventListener 切换此操作。 It works for the div 'moreno' but fails to work for the other locations.它适用于 div 'moreno' 但不适用于其他位置。

Thank you for your help in advance.提前谢谢你的帮助。

The const iconText = document.querySelector('.map-icon-text'); const iconText = document.querySelector('.map-icon-text'); is global, so only return the first match on the web page.是全局的,所以只返回 web 页面上的第一个匹配项。

For it to work, you want to rely on the event target specificially:要使其正常工作,您需要特别依赖事件目标:

function addSpanClass(event) {
  const iconText = event.target.querySelector('.map-icon-text');
  iconText.classList.add('map-icon-text-active');
}

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

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