为每个产品JS创建事件监听器

发布于 2025-01-10 00:18:58 字数 2142 浏览 1 评论 0原文

我必须构建一个事件侦听器来侦听单击以删除其链接到的产品;为此,我被要求使用 element.closest()

以下是为每个产品生成的 HTML:

<article class="cart__item" data-id="${product._id}" data-color="${chosenProduct.color}">
  <div class="cart__item__img">
    <img src="${product.imageUrl}" alt="${product.altTxt}">
  </div>
  <div class="cart__item__content">
    <div class="cart__item__content__description">
      <h2>${product.name}</h2>
      <p>${chosenProduct.color}</p>
      <p>${product.price}€</p>
    </div>
    <div class="cart__item__content__settings">
      <div class="cart__item__content__settings__quantity">
        <p>Qté : ${quantity}</p>
        <input type="number" class="itemQuantity" name="itemQuantity" min="1" max="100" value="${quantity}">
      </div>
      <div class="cart__item__content__settings__delete">
        <p class="deleteItem">Supprimer</p>
      </div>
    </div>
  </div>
</article>

如您所见,产品的标识位于文章标签内。

我使用事件监听器延迟启动我的函数,以确保创建 HTML,以便我可以收集按钮。

window.setTimeout(function deleteButton() {
  const buttons = document.querySelectorAll(".deleteItem");

  buttons.forEach((button) => {
    button.addEventListener("click", deleteProduct);
  });
}, 800);

使用此代码,每个按钮都会响应该功能,但它们只删除第一个产品。

这是删除函数的样子:

function deleteProduct() {
  const itemToDelete = document.querySelector(".cart__item__content");
  const idProductToDelete = itemToDelete.closest("article").getAttribute("data-id");
  const colorProductToDelete = itemToDelete.closest("article").getAttribute("data-color");
  const productToDelete = "product-" + idProductToDelete + "-" + colorProductToDelete;
  //remove the item from local storage
  localStorage.removeItem(productToDelete);
  //remove from the html instantly
  deleteHtml();
}

我的理解是: element.closest() 仅适用于 querySelector() (我无法让它与getElements 等...),但 querySelector() 仅返回它找到的第一个元素。

我怎样才能做到这一点?

I have to build an event listener that listens for a click to delete the product it's linked to; for this I was asked to use element.closest().

Here is the HTML that is being generated for each product:

<article class="cart__item" data-id="${product._id}" data-color="${chosenProduct.color}">
  <div class="cart__item__img">
    <img src="${product.imageUrl}" alt="${product.altTxt}">
  </div>
  <div class="cart__item__content">
    <div class="cart__item__content__description">
      <h2>${product.name}</h2>
      <p>${chosenProduct.color}</p>
      <p>${product.price}€</p>
    </div>
    <div class="cart__item__content__settings">
      <div class="cart__item__content__settings__quantity">
        <p>Qté : ${quantity}</p>
        <input type="number" class="itemQuantity" name="itemQuantity" min="1" max="100" value="${quantity}">
      </div>
      <div class="cart__item__content__settings__delete">
        <p class="deleteItem">Supprimer</p>
      </div>
    </div>
  </div>
</article>

As you can see, the identity of the product is inside the article tag.

I launch my function with the event listener with a delay, to make sure the HTML is created, so that I can collect the button(s).

window.setTimeout(function deleteButton() {
  const buttons = document.querySelectorAll(".deleteItem");

  buttons.forEach((button) => {
    button.addEventListener("click", deleteProduct);
  });
}, 800);

With this code, each button responds to the function, but they only delete the first product.

Here is what the delete function looks like:

function deleteProduct() {
  const itemToDelete = document.querySelector(".cart__item__content");
  const idProductToDelete = itemToDelete.closest("article").getAttribute("data-id");
  const colorProductToDelete = itemToDelete.closest("article").getAttribute("data-color");
  const productToDelete = "product-" + idProductToDelete + "-" + colorProductToDelete;
  //remove the item from local storage
  localStorage.removeItem(productToDelete);
  //remove from the html instantly
  deleteHtml();
}

What I understand is that: element.closest() only works with querySelector() (I can't get it to work with getElements etc...), but querySelector() returns only the first element it finds.

How can I make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2025-01-17 00:18:58
    function deleteProduct() {
  const itemToDelete = event.target.querySelector(".cart__item__content");
  const idProductToDelete = itemToDelete.closest("article").getAttribute("data-id");
  const colorProductToDelete = itemToDelete.closest("article").getAttribute("data-color");
  const productToDelete = "product-" + idProductToDelete + "-" + colorProductToDelete;
  //   //remove the item from local storage
  localStorage.removeItem(productToDelete);
  //   //remove from the html instantly
  deleteHtml();
}

您可以使用单击事件的目标元素,而不是使用 document.querySelector。最接近的就可以了。

或者你可以直接使用

const idProductToDelete = event.target.closest("article").getAttribute("data-id");
    function deleteProduct() {
  const itemToDelete = event.target.querySelector(".cart__item__content");
  const idProductToDelete = itemToDelete.closest("article").getAttribute("data-id");
  const colorProductToDelete = itemToDelete.closest("article").getAttribute("data-color");
  const productToDelete = "product-" + idProductToDelete + "-" + colorProductToDelete;
  //   //remove the item from local storage
  localStorage.removeItem(productToDelete);
  //   //remove from the html instantly
  deleteHtml();
}

Instead of using document.querySelector, you can use the click event's target element. closest would work for it.

or you can directly use

const idProductToDelete = event.target.closest("article").getAttribute("data-id");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文