【问题标题】:Trying to clear all customers on clicking the clear button. Also remove a customer on deleting a customer试图通过单击清除按钮清除所有客户。在删除客户时也删除客户
【发布时间】:2021-02-10 12:05:08
【问题描述】:

https://github.com/Rengkat/OOP-Customer-List.git

尝试在单击清除按钮时清除所有客户。此外,删除客户时删除客户

【问题讨论】:

  • 您能否提供更多有关您遇到的错误的详细信息?
  • 尝试在单击清除按钮时清除所有客户。此外,在删除客户时删除客户。我试试,但按钮不起作用。代码在我的 Github 上
  • 我的回答能满足您的要求吗?

标签: javascript class constructor click


【解决方案1】:

为了快速修复,每次添加客户时,您还应该为父 div 提供一个与其客户 ID 匹配的 ID

    //new div for a customer
    const newDiv = document.createElement("div");
    //seting a class for the div
    newDiv.className = "flex-child";
    newDiv.id = customer.id;    //Here

和数据删除客户按钮

<button class="button u-full-width customer-btn" data-cust="${customer.id}">
    DELETE CUSTOMER
</button>`;

这样,您可以轻松获取数据并找到删除客户的组件

document.addEventListener('click', function(event) {
  if (event.target.classList.contains("customer-btn")) {
    let cust = event.target.dataset.cust;
    document.getElementById(cust).remove();
  }
});

删除所有客户更容易,只需清除其 innerHTML 即可完成

  deletAllCustomers() {
    document.getElementById("row").innerHTML = "";
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2011-07-13
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2019-03-22
    • 2020-09-07
    相关资源
    最近更新 更多