js函数有时仅在firefox中有效,但在我的其他浏览器中始终有效

时间:2019-07-19 00:53:24

标签: javascript html json ajax

因此“添加到购物车”功能有时仅在Firefox中有效,而其他浏览器可以正常工作

更改某些侦听器代码

var addToCartButtons = document.getElementsByClassName('shop-item-button')
    for (var i = 0; i < addToCartButtons.length; i++) {
        var button = addToCartButtons[i]
        button.addEventListener('click', addToCartClicked)
    }

    document.getElementsByClassName('btn-purchase')[0].addEventListener('click', purchaseClicked)
}

//-----------------


function addToCartClicked(event) {
    var button = event.target
    var shopItem = button.parentElement.parentElement
    var title = shopItem.getElementsByClassName('shop-item-title')[0].innerText

    var selectedValue = shopItem.getElementsByClassName('shop-item-list')[0]
    var selectedSize = selectedValue.options[selectedValue.selectedIndex].text

    var price = shopItem.getElementsByClassName('shop-item-price')[0].innerText
    var imageSrc = shopItem.getElementsByClassName('shop-item-image')[0].src
    addItemToCart(title, price, imageSrc, selectedSize)
    updateCartTotal()
}

function addItemToCart(title, price, imageSrc, selectedValue) {
    var cartRow = document.createElement('div')
    cartRow.classList.add('cart-row')
    var cartItems = document.getElementsByClassName('cart-items')[0]
    var cartItemNames = cartItems.getElementsByClassName('cart-item-title')
     var cartItemNames = cartItems.getElementsByClassName('cart-item-list')
    for (var i = 0; i < cartItemNames.length; i++) {
        if (cartItemNames[i].innerText == title) {
            alert('This item is already added to the cart')
            return
        }
    }

用于将商品添加到购物车 https://www.jagsblast.xyz/public/rio-roller2.html

0 个答案:

没有答案