我正在嘗試設置它,以便如果我單擊具有“addtocart”類的按鈕,它將檢查 HTML 文檔中按鈕的 id 並遍歷產品數組對象,直到找到具有相同 Id 的對象元素上的按鈕,然後將其推送到購物車數組。然後我需要更改“cartamnt”跨度的內部文本,以便根據添加到購物車的商品數量,文本“0 購物車”變為“1 購物車”等。
//Arrays
var products = [{
Price: 20,
Name: "Football Helmet",
Description: "Titans football helmet",
Id: "Titan_a"
},
{
Price: 15,
Name: "Light Blue Shirt",
Description: "Titans light blue shirt",
Id: "Titan_b"
},
{
Price: 15,
Name: "White Shirt",
Description: "Titans white shirt",
Id: "Titan_c"
},
{
Price: 15,
Name: "Blue Shirt",
Description: "Titans blue shirt",
Id: "Titan_d"
},
{
Price: 25,
Name: "Hockey Stick",
Description: "Titans hockey stick",
Id: "Titan_d"
}
];
var cart = [];
//All the button variables
var addtocart = document.getElementsByClassName("add");
var camount = document.getElementById("cartamnt");
cartamnt.innerText = "0 cart"
addtocart.onclick = function(products, cart) {
for (var i = 0; i < products.length; i++) {
if (addtocart.Id == products.Id) {
cart.push(products.Id[i])
cartamnt.innerText = i++ + "cart"
}
}
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1 id="main_title">Titan Sports and Apparel LLC</h1>
<div class="main">
<div class="row">
<span href="checkout.html" id="cartamnt">0 cart</span>
<br />
</div>
<div class="row">
<div class="column">
<h1>Football helmet</h1>
<img id="img" src="img/Helmet_A.jpg">
<p>Price: $20</p>
<p>Official Titan Sportswear Helmet</p>
<button class="add" id="Titan_a">Add to cart</button>
<button id="Remove_a">Remove from cart</button>
</div>
<div class="column">
<h1>Light Blue Shirt</h1>
<img id="img" src="img/Shirt_A.jpg">
<p>Price: $15</p>
<p>Light blue cotton material Titans shirt</p>
<button class="add" id="Titan_b">Add to cart</button>
<button id="Remove_b">Remove from cart</button>
</div>
<div class="column">
<h1>White Shirt</h1>
<img id="img" src="img/Shirt_B.jpg">
<p>Price: $15</p>
<p>White cotton material Titans shirt</p>
<button class="add" id="Titan_c">Add to cart</button>
<button id="Remove_c">Remove from cart</button>
</div>
<div class="column">
<h1>Blue Shirt</h1>
<img id="img" src="img/Shirt_C.jpg">
<p>Price: $15</p>
<p>Blue cotton material Titans shirt</p>
<button class="add" id="Titan_d">Add to cart</button>
<button id="Remove_d">Remove from cart</button>
</div>
<div class="column">
<h1>Hockey Stick</h1>
<img id="img" src="img/Stick_A.png">
<p>Price: $25</p>
<p>Black Titans hockey stick</p>
<button class="add" id="Titan_e">Add to cart</button>
<button id="Remove_e">Remove from cart</button>
</div>
</div>
</div>
<a href="index.html">Homepage</a>
<footer>© Titan Sports and Apparel LLC | Email: [email protected]</footer>
<script src="js/shoppingCart.js"></script>
</body>
</html>
你的代碼有點亂。我對它進行了一些清理以幫助您入門,但我真的建議您將其發佈到Code Review以了解如何改進它。
首先要注意的是,您正在使用id
“添加”按鈕來存儲產品的 id。這是一個問題,原因有兩個:
id
屬性的目的是給元素一個唯一的標識符,而不是存儲數據。id
與“添加”按鈕相同,這是不正確的,因為它id
應該是唯一的。另一種方法是給它一個不同的 id,並以某種方式在 JS 中從中提取產品 id,這真的很複雜。這就是為什麼我們將使用數據屬性而不是id
存儲產品 ID。例如:
<button class="add" data-product="Titan_a">Add to cart</button>
<button class="remove" data-product="Titan_a">Remove from cart</button>
現在,您的 JS 的問題在於您的addtocart
變量實際上是所有按鈕的數組,而不是單個按鈕,因此您不能僅將onclick
事件附加到它。您需要遍歷所有按鈕並將onclick
事件單獨附加到它們。
所以我們會得到這樣的東西:
const addButtons = document.getElementsByClassName("add");
const removeButtons = document.getElementsByClassName("remove");
for (const addButton of addButtons) {
addButton.addEventListener("click", () => {
...
});
}
for (const removeButton of removeButtons) {
removeButton.addEventListener("click", () => {
...
});
}
現在很容易實現添加和刪除。您不需要手動循環拋出您的數組,您可以簡單地使用該find
函數和其他一些有用的“內置”數組函數。
這是一個完整的工作示例:
let products = [{
Price: 20,
Name: "Football Helmet",
Description: "Titans football helmet",
Id: "Titan_a"
},
{
Price: 15,
Name: "Light Blue Shirt",
Description: "Titans light blue shirt",
Id: "Titan_b"
},
{
Price: 15,
Name: "White Shirt",
Description: "Titans white shirt",
Id: "Titan_c"
},
{
Price: 15,
Name: "Blue Shirt",
Description: "Titans blue shirt",
Id: "Titan_d"
},
{
Price: 25,
Name: "Hockey Stick",
Description: "Titans hockey stick",
Id: "Titan_e"
}
];
let cart = [];
const addButtons = document.getElementsByClassName("add");
const removeButtons = document.getElementsByClassName("remove");
const amountLabel = document.getElementById("cartamnt");
for (const addButton of addButtons) {
addButton.addEventListener("click", () => {
let product = products.find(p => p.Id == addButton.dataset.product);
cart.push(product);
amountLabel.innerText = cart.length + "items";
});
}
for (const removeButton of removeButtons) {
removeButton.addEventListener("click", () => {
const index = cart.findIndex(p => p.Id === removeButton.dataset.product); // index of product to remove
cart.splice(index, 1); // removing it
amountLabel.innerText = cart.length + "items";
});
}
<!DOCTYPE html>
<html>
<body>
<h1 id="main_title">Titan Sports and Apparel LLC</h1>
<div class="main">
<div class="row">
<span href="checkout.html" id="cartamnt">0 cart</span>
<br />
</div>
<div class="row">
<div class="column">
<h1>Football helmet</h1>
<img id="img" src="img/Helmet_A.jpg">
<p>Price: $20</p>
<p>Official Titan Sportswear Helmet</p>
<button class="add" data-product="Titan_a">Add to cart</button>
<button class="remove" data-product="Titan_a">Remove from cart</button>
</div>
<div class="column">
<h1>Light Blue Shirt</h1>
<img id="img" src="img/Shirt_A.jpg">
<p>Price: $15</p>
<p>Light blue cotton material Titans shirt</p>
<button class="add" data-product="Titan_b">Add to cart</button>
<button class="remove" data-product="Titan_b">Remove from cart</button>
</div>
<div class="column">
<h1>White Shirt</h1>
<img id="img" src="img/Shirt_B.jpg">
<p>Price: $15</p>
<p>White cotton material Titans shirt</p>
<button class="add" data-product="Titan_c">Add to cart</button>
<button class="remove" data-product="Titan_c">Remove from cart</button>
</div>
<div class="column">
<h1>Blue Shirt</h1>
<img id="img" src="img/Shirt_C.jpg">
<p>Price: $15</p>
<p>Blue cotton material Titans shirt</p>
<button class="add" data-product="Titan_d">Add to cart</button>
<button class="remove" data-product="Titan_d">Remove from cart</button>
</div>
<div class="column">
<h1>Hockey Stick</h1>
<img id="img" src="img/Stick_A.png">
<p>Price: $25</p>
<p>Black Titans hockey stick</p>
<button class="add" data-product="Titan_e">Add to cart</button>
<button class="remove" data-product="Titan_e">Remove from cart</button>
</div>
</div>
</div>
<a href="index.html">Homepage</a>
<footer>© Titan Sports and Apparel LLC | Email: [email protected]</footer>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句