为什么所有的div都有相同的名字?

时间:2018-02-20 13:08:40

标签: javascript arrays loops

我想在点击产品按钮后删除,但它无法正常工作我不知道为什么。它不会从表中删除正确的对象

document.addEventListener('click', function(e) {
  if (e.target.id === 'removeItem') {
    var divWithItem = document.getElementsByClassName('containerBasket__allProducts__product');
    
    // Pobieram name itemu
    var nameItem = e.target.parentElement.parentElement.getAttribute('name');
    var thisItemDiv = e.target.parentElement.parentElement;
    var thisItem = JSON.parse(products[nameItem]);
    
    products.splice(thisItem, 1);
    localStorage.setItem('product', JSON.stringify(products));
    thisItemDiv.remove();
    
    for (var i = 0; i < products.length; i++) {
      for (var j = 0; j < divWithItem.length; j++) {
        divWithItem[j].setAttribute('name', [j]);
      }
    }
    console.log(products);
  }
});

循环之前

enter image description here

循环后

enter image description here

为什么同名的div?

0 个答案:

没有答案
相关问题