簡體   English   中英

如何在JavaScript中刪除動態創建的節點

[英]How to remove a dynamically created node in Javascript

我正在用JavaScript創建一個簡單的購物車網頁作為作業。 我正在為購物車中的每本書創建一個“刪除項目”按鈕,但是我還不太清楚如何實現它。 我知道這將需要使用DOM樹,但是我不知道如何使用。 以下是相關代碼:

<script>

function addToCart(bookToAdd, priceToAdd)
{
    //Create the node
    var bookInfo = document.createElement();
    bookInfoString = "Book: " + document.getElementById(bookToAdd).innerHTML + 
        " <br />Price: $" + document.getElementById(priceToAdd).innerHTML + 
        '<br /> <button type="button" onclick="removeFromCart(' + bookToAdd + ', ' + priceToAdd + ')">Remove from cart!</button> <br /> <br />';
    bookInfo.innerHTML = bookInfoString;
    //Append the node
    document.getElementById('cartItems').appendChild(bookInfo); 
    //Update the cart total
    document.getElementById('totalPrice').firstChild.nodeValue = (parseFloat(document.getElementById('totalPrice').innerHTML) + parseFloat(document.getElementById(priceToAdd).innerHTML)).toFixed(2);
}

function removeFromCart(bookToRemove, priceToRemove)
{
    //Remove the node
    getElementById('cartItems').removeChild(bookToRemove);
    //Update the cart total
    document.getElementById('totalPrice').firstChild.nodeValue = (parseFloat(document.getElementById('totalPrice').innerHTML) - parseFloat(document.getElementById(priceToRemove).innerHTML)).toFixed(2);
}

</script>

... html代碼的相關部分:

    <tr>
        <!--Fill in the first book information-->
        <td> <span class="bookName" id="book1"><b>Artificial Intelligence: A Modern Approach</b></span><br>
        <img src="AI.jpeg" alt="AI Book Cover" width="100"> <br>
        <b>ISBN:</b> 978-0-13-604259-4 <br>
        <b>Description:</b> Artificial Intelligence: A Modern Approach, 3e offers the most comprehensive, 
        up-to-date introduction to the theory and practice of artificial intelligence. Number one in 
        its field, this textbook is ideal for one or two-semester, undergraduate or graduate-level 
        courses in Artificial Intelligence. <br>
        <b>PRICE: $</b> <span id="price1"> 158.55 </span> 
        <button type="button" onclick="addToCart('book1', 'price1')">Add to cart!</button>
        </td>

        <!-- Create the shopping cart area of the page-->
        <td rowspan="10"> 
        <!-- Create the space for the items in the cart to be written -->
        <div id="cartItems"></div>

        <!-- Create the space for the Total Price to be written -->
        <b>Total Price: $<div style="display: inline" id="totalPrice">0</div></b>
        </td>

    </tr>

除了刪除條目外,其他所有功能都起作用。 我特別難以解決如何訪問需要刪除的特定節點。 上面顯示的關於人工智能書的購物車中的所有條目都是用相同的ID('book1')創建的,所以我不知道該如何挑選。

提前致謝。

試試這個-我給它一個唯一的ID,但是因為我刪除了parentNode,所以實際上並不需要它

function addToCart(bookToAdd, priceToAdd) {
    //Create the node
    var bookInfo = document.createElement("div");
    bookInfoString = "Book: " + document.getElementById(bookToAdd).innerHTML + 
        " <br />Price: $" + document.getElementById(priceToAdd).innerHTML + 
        '<br /> <button type="button" onclick="removeFromCart(this.parentNode,'+ 
         priceToAdd + ')">Remove from cart!</button> <br /> <br />';
    bookInfo.innerHTML = bookInfoString;
    bookInfo.id=bookToAdd+'_'+new Date().getTime();
    //Append the node
    document.getElementById('cartItems').appendChild(bookInfo); 
    //Update the cart total
    document.getElementById('totalPrice').innerHTML = (parseFloat(document.getElementById('totalPrice').innerHTML) + parseFloat(document.getElementById(priceToAdd).innerHTML)).toFixed(2);
}

function removeFromCart(bookToRemove, priceToRemove) {
    //Remove the node
    getElementById('cartItems').removeChild(bookToRemove);
    //Update the cart total
    document.getElementById('totalPrice').innerHTML = (parseFloat(document.getElementById('totalPrice').innerHTML) - parseFloat(document.getElementById(priceToRemove).innerHTML)).toFixed(2);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM