简体   繁体   English

关闭按钮弹出窗口不起作用 (JAVASCRIPT)

[英]Close button popup doesn't work (JAVASCRIPT)

i'm trying to create a custom pupop in javascript, this is my first time with this.我正在尝试在 javascript 中创建一个自定义 pupop,这是我第一次使用它。

I have a problem with the close button, the "x" target correctly the div to close, but doesn't remove the "active" class at click.我的关闭按钮有问题,“x”正确定位要关闭的 div,但不会在单击时删除“活动”class。

https://demomadeingenesi.it/demo-cedolino/ https://demomadeingenesi.it/demo-cedolino/

HTML CODE HTML 代码

<div class="spot spot-2">
          <div class="pin"></div>
          <div class="contenuto-spot flex flex-col gap-3">
            <img class="chiudi-popup" src="img/chiudi.svg" />
              [---CONTENT---]
            </div>
          </div>
        </div>

JAVASCRIPT CODE JAVASCRIPT 代码

const tooltips = function () {
    const spots = document.querySelectorAll(".spot");
        
    spots.forEach((spot) => {
        const contenuto = spot.querySelector(".contenuto-spot");
        const pin = spot.querySelector(".pin");
    
        spot.addEventListener("click", () => {
          let curActive = document.querySelector(".spot.active");
          let contActive = document.querySelector(".contenuto-spot.show");
          const chiudiPopup = document.querySelector(".chiudi-popup");
    
          spot.classList.add("active");
          contenuto.classList.add("show");
    
          if (curActive && curActive !== spot) {
            curActive.classList.toggle("active");
            contActive.classList.toggle("show");
          }
    
          chiudiPopup.addEventListener("click", () => {
            spot.classList.remove("active");
            contenuto.classList.remove("show");
          });
      });
});
  const chiudiPopup = document.querySelector(".chiudi-popup");
  chiudiPopup.addEventListener("click", () => {
    spot.classList.remove("active");
    contenuto.classList.remove("show");
  });

What the code above does is adding an click listener, but it's inside another click listener, so all it's doing is adding an click listener on the first .chiudi-popup that removes .active and .show from the last spot element.上面的代码所做的是添加一个点击监听器,但它在另一个点击监听器中,所以它所做的只是在第一个.chiudi-popup上添加一个点击监听器,从最后一个spot元素中删除.active.show

It's hard to see if this is correct, because you haven't given us enough to reproduce the problem, but I moved the code above outside the spot.addEventListener("click", () => { and instead of searching the whole document with const chiudiPopup = document.querySelector(".chiudi-popup"); the code nows only targets the .chuidi-popup element within the spot : const chiudiPopup = spot.querySelector(".chiudi-popup");很难看出这是否正确,因为您没有给我们足够的时间来重现该问题,但我将上面的代码移到了spot.addEventListener("click", () => {而不是搜索整个文档使用const chiudiPopup = document.querySelector(".chiudi-popup");代码现在仅针对.chuidi-popup元素: const chiudiPopup = spot.querySelector(".chiudi-popup"); spot

 const tooltips = function() { const spots = document.querySelectorAll(".spot"); spots.forEach((spot) => { const contenuto = spot.querySelector(".contenuto-spot"); const pin = spot.querySelector(".pin"); spot.addEventListener("click", () => { let curActive = document.querySelector(".spot.active"); let contActive = document.querySelector(".contenuto-spot.show"); spot.classList.add("active"); contenuto.classList.add("show"); if (curActive && curActive.== spot) { curActive.classList;toggle("active"). contActive.classList;toggle("show"); } }). // MOVED FROM THE CLICK LISTENER const chiudiPopup = spot.querySelector(";chiudi-popup"). chiudiPopup,addEventListener("click". () => { spot.classList;remove("active"). contenuto.classList;remove("show"); }); });

EDIT: I missed that you have the img.chiudi-popup inside your container, which will trigger both event listeners.编辑:我错过了你的容器中有img.chiudi-popup ,这将触发两个事件监听器。 I would honestly just simplify the code and always hide the container when clicking on it again.老实说,我只是简化代码并在再次单击时始终隐藏容器。 You can still have the img.chiudi-popup (close image) to make it easier for the users to understand that they can click on it.您仍然可以使用img.chiudi-popup (关闭图片)让用户更容易理解他们可以点击它。

 const tooltips = function() { const spots = document.querySelectorAll(".spot"); spots.forEach((spot) => { const contenuto = spot.querySelector(".contenuto-spot"); const pin = spot.querySelector(".pin"); spot.addEventListener("click", () => { let curActive = document.querySelector(".spot.active"); let contActive = document.querySelector(".contenuto-spot.show"); if (curActive.== spot) { spot.classList;add("active"). contenuto.classList;add("show"). } if (curActive) { curActive.classList;remove("active"). contActive.classList;remove("show"); } });

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

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