如何从已删除的元素中进行console.log的innerText记录?

时间:2019-09-18 21:15:42

标签: javascript html

在我的updateCartTotal()函数中,我想console.log从购物车中删除的元素。

每次按下删除按钮时,我希望它打印出itemprice。但是,每当我执行此操作时,它都会返回未定义。在某些情况下,它只能识别1个项目。我认为我的JavaScript代码没有问题,而是我的HTML

const updateCartTotal = () => {
    var cartItemContainer = document.getElementsByClassName('cart-items')[0]
    var cartRows = cartItemContainer.getElementsByClassName('cart-row')

    for(var i = 0; i < cartRows.length; i++){
        var cartRow = cartRows[i];
        var itemElement= cartRow.getElementsByClassName('cart-item')[0].innerHTML
        var priceElement = cartRow.getElementsByClassName('cart-price')[0].innerHTML
        var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]

        var item = itemElement.innerText
        var price = priceElement.innerText
        // var quantity = quantityElement.value
        console.log(item, price)
    }
}
<section class="container shopping-cart">
    <div class="title">Cart</div>
    <div class="cart-row">
        <span class="cart-item cart-header cart-column">ITEM</span>
        <span class="cart-price cart-header cart-column">PRICE</span>
        <span class="cart-quantity cart-header cart-column">QUANTITY</span>
    </div>
    <div class="cart-items">
        <div class="cart-row">
            <div class="cart-item">
                <span class="cart-item-title">Item 1</span>
            </div>
            <span class="cart-price ">$14.99</span>
            <div class="cart-quantity-input">
                <input type="number" value="1">
                <button class="btn btn-danger ml-5" type="button">REMOVE</button>
            </div>
        </div>
        <div class="cart-row">
            <div class="cart-item">
                <span class="cart-item-title ">Item 2</span>
            </div>
            <span class="cart-price">$79.99</span>
            <div class="cart-quantity-input">
                <input type="number" value="2">
                <button class="btn btn-danger ml-5" type="button">REMOVE</button>
            </div>
        </div>
    </div>
</section>

2 个答案:

答案 0 :(得分:0)

我看不到您当前的代码如何在控制台中记录基于按钮单击的值。因此,我创建了功能removeItem()来实现所需的功能。使用此解决方案将功能添加到代码中。

有关解决方法,请参见密码笔。

https://codepen.io/ianroskow/pen/WNeagXQ

在单击“删除”按钮时,我传递上下文,然后遍历dom以控制台记录所需的值。

我用最接近的方法来找到父节点,并使用查询选择器来找到子节点。

https://developer.mozilla.org/en-US/docs/Web/API/Element/closest

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

 const updateCartTotal = () => {

            var cartItemContainer = document.getElementsByClassName('cart-items')[0]
            var cartRows = cartItemContainer.getElementsByClassName('cart-row')
            for(var i = 0; i < cartRows.length; i++){
                var cartRow = cartRows[i];
                var itemElement= cartRow.getElementsByClassName('cart-item')[0].innerHTML
                var priceElement = cartRow.getElementsByClassName('cart-price')[0].innerHTML
                var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]

                var item = itemElement.innerText
                var price = priceElement.innerText
                // var quantity = quantityElement.value
                console.log(item, price)
            }

        }
 
 function removeItem(element){
   const cartRow = element.closest(".cart-row");
   console.log(cartRow.querySelector(".cart-item-title").innerHTML);
   console.log(cartRow.querySelector(".cart-price").innerHTML);
   cartRow.parentNode.removeChild(cartRow);
 }
    <section class="container shopping-cart">
            <div class="title">Cart</div>
            <div class="cart-row">
                <span class="cart-item cart-header cart-column">ITEM</span>
                <span class="cart-price cart-header cart-column">PRICE</span>
                <span class="cart-quantity cart-header cart-column">QUANTITY</span>
            </div>
            <div class="cart-items">
                <div class="cart-row">
                    <div class="cart-item">
                        <span class="cart-item-title">Item 1</span>
                    </div>
                    <span class="cart-price ">$14.99</span>
                    <div class="cart-quantity-input">
                        <input type="number" value="1">
                        <button class="btn btn-danger ml-5" onclick="removeItem(this)"  type="button">REMOVE</button>
                    </div>
                </div>
                <div class="cart-row">
                    <div class="cart-item">
                        <span class="cart-item-title ">Item 2</span>
                    </div>
                    <span class="cart-price">$79.99</span>
                    <div class="cart-quantity-input">
                        <input type="number" value="2">
                        <button class="btn btn-danger ml-5" onclick="removeItem(this)" type="button">REMOVE</button>
                    </div>
                </div>
            </div>
  </section>

答案 1 :(得分:0)

itemprice将不是元素。它们是代表元素的innerHTML的字符串。
要获取商品名称,只需使用cartRow.getElementsByClassName('cart-item-title')[0].innerHTML
要获取价格,请使用cartRow.getElementsByClassName('cart-price')[0].innerHTML

请勿使用innerText,因为itemprice已经是字符串,这将导致错误。

const updateCartTotal = () => {
  var cartItemContainer = document.getElementsByClassName('cart-items')[0]
  var cartRows = cartItemContainer.getElementsByClassName('cart-row')
  
  for (var i = 0; i < cartRows.length; i++) {
    var cartRow = cartRows[i];
    var item = cartRow.getElementsByClassName('cart-item-title')[0].innerHTML
    var price = cartRow.getElementsByClassName('cart-price')[0].innerHTML
    var quantity = cartRow.getElementsByClassName('cart-quantity-input')[0]
    
    console.log(" Item: " + item + "\n" +
                "Price: " + price)
  }
}
updateCartTotal();
<section class="container shopping-cart">
  <div class="title">Cart</div>
  <div class="cart-row">
    <span class="cart-item cart-header cart-column">ITEM</span>
    <span class="cart-price cart-header cart-column">PRICE</span>
    <span class="cart-quantity cart-header cart-column">QUANTITY</span>
  </div>
  <div class="cart-items">
    <div class="cart-row">
      <div class="cart-item">
        <span class="cart-item-title">Item 1</span>
      </div>
      <span class="cart-price ">$14.99</span>
      <div class="cart-quantity-input">
        <input type="number" value="1">
        <button class="btn btn-danger ml-5" type="button">REMOVE</button>
      </div>
    </div>
    <div class="cart-row">
      <div class="cart-item">
        <span class="cart-item-title">Item 2</span>
      </div>
      <span class="cart-price">$79.99</span>
      <div class="cart-quantity-input">
        <input type="number" value="2">
        <button class="btn btn-danger ml-5" type="button">REMOVE</button>
      </div>
    </div>
  </div>
</section>