当前位置:优学网  >  在线题库

JavaScript函数为't工作,但我'我确信一切'很好

发表时间:2022-07-18 00:28:14 阅读:77

我不知道该怎么办,因为一切都很好,但javascript中的最后一个函数不起作用.我确信一切都写得很好,而且我不得不改变一个函数,即使我确信它写得很好(评论的一个).我不确定这是因为我的java版本还是?


    <html>
    <head>
        <title>Pijaca</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>

        <h1>Pijaca</h1>

        <div class="container">
            <div class="items">
                <h2>Povrće</h2>

                <div class="single-item">
                    <img src="img/potato.png">
                    <div class="si-content">
                        <h3>Krompir</h3>
                        <p class="price">$10</p>
                    </div>
                    <div class="actions">
                        <input type="number" name="quantity" value="0" min="0">
                        <button onclick="addToCart(this)">Dodaj</button>
                    </div>
                </div>

                <div class="single-item">
                    <img src="img/tomato.png">
                    <div class="si-content">
                        <h3>Paradajz</h3>
                        <p class="price">$20</p>
                    </div>
                    <div class="actions">
                        <input type="number" name="quantity" value="0" min="0">
                        <button onclick="addToCart(this)">Dodaj</button>
                    </div>
                </div>

                <div class="single-item">
                    <img src="img/carrot.png">
                    <div class="si-content">
                        <h3>Šargarepa</h3>
                        <p class="price">$30</p>
                    </div>
                    <div class="actions">
                        <input type="number" name="quantity" value="0" min="0">
                        <button onclick="addToCart(this)">Dodaj</button>
                    </div>
                </div>
            </div>

            <div class="cart">
                <h2>Korpa</h2>

                <div class="cart-items"></div>

                <div class="total"></div>
            </div>
        </div>

        <script>
    let AllTotal = 0;

    function addToCart (elem){
        let mainEl = elem.closest('.single-item');
        let quantity = mainEl.querySelector('input').value;
        let name = mainEl.querySelector('.single-item h3').innerText;
        let price = mainEl.querySelector('.price').innerText;

        price = parseInt(price.substring(1));

        if(quantity == 0){
            alert('Dodajte kolicinu!');
            return;
    }

        let total = price * quantity;

        document.querySelector('.cart-items').innerHTML += `<div class"cart-single-item">
        <h3>${name}:</h3> <p>${quantity} x $${price} = $<span>${total}</span></p> 
        <button onclick="remove(this)" class="remove-item">Ukloni</button>
        </div>`

        elem.setAttribute('disabled', 'true');
        elem.innerText ='Dodato'

        AllTotal += total;

文件查询选择器(总计).innerText=`总计:$${AllTotal}`
}

    function remove(element){

        let mainEl = element.parentElement;
    // this part won't work as let mainEl = element.closest('.single-cart-item');
        let price = parseInt(mainEl.querySelector('p span').innerText);
        let name = mainEl.querySelector('h3').innerText;
        let vegetables = document.querySelectorAll('.single-item')

        AllTotal -= price;

文件查询选择器(总计).innerText=总计:$${AllTotal}

要素删除属性("Disabled") 缅因州.删除()

**蔬菜.forEach(函数(vege){ if(植物查询选择器(.si含量h3).innerText===名称){

素食.查询选择器(.actions按钮).setAttribute(禁用,false) 素食.查询选择器(.actions按钮).innerText="DoDAJ" 素食.querySelector(操作输入).值=0 } })**

} &lt/script&gt &lt/body&gt &lt/html&gt

🎖️ 优质答案
相关问题