TypeError:无法读取未定义的属性“ innerText”-从eshop中删除

我需要在这里获得帮助。我正在我的网上商店工作。除了从购物车中删除一个物体外,其他所有东西都运行良好。当我从购物车中删除一个对象时,价格不变。并在控制台中写入“ Uncaught TypeError:无法读取未定义的属性'innerText'     在updateCelkoveCeny(Eshop.js:86)处,该86行是parseFloat所在的行有什么建议吗?

function updateCelkoveCeny() {
var produktyKosikContainer = document.getElementsByClassName('kosik-produkty')[0]
var radkyKosik = produktyKosikContainer.getElementsByClassName('kosik-radek')
var celkovaCena = 0
for (var i = 0; i < radkyKosik.length; i++) {
    var radekKosik = radkyKosik[i]
    var cenaElement = radekKosik.getElementsByClassName('kosik-cena')[0]
    var pocetElement = radekKosik.getElementsByClassName('kosik-pocet-input')[0]
    var cena = parseFloat(cenaElement.innerText.replace(',-', ''))
    var pocet = pocetElement.value
    celkovaCena = celkovaCena + (cena * pocet)
}
document.getElementsByClassName('kosik-celkem-cena')[0].innerText = celkovaCena + ",-"

}

评论
  • et_qui
    et_qui 回复

    您定位DOM元素而不是JS函数。您可以通过以下方式遍历数组并获取innerText:

    var cenaElement = document.getElementsByClassName("kosik-cena");
    
    for (var i = 0; i < cenaElement.length; i++) {
      var text = cenaElement[i].innerText;
      console.log(text);
    }
    

    Another possible problem could be that there is no element found with class 'kosik-cena'. This way it is not possible to get the innerText of an element that doesn't exist. It would be nice to give some insights into your HTML.