js购物车
时间: 2023-07-23 13:04:22 浏览: 56
好的,你需要一个简单的 JS 购物车功能,是吗?那么你需要考虑以下几个方面:
1. 商品信息:你需要定义每个商品的名称、价格、数量等信息。最好使用一个 JavaScript 对象来存储这些信息。
2. 购物车数据结构:你需要确定购物车的数据结构,例如,你可以使用一个数组来存储购物车中的商品信息。
3. 添加商品:当用户点击“添加到购物车”按钮时,你需要将商品信息添加到购物车数组中。
4. 计算总价:每当用户添加或删除商品时,你需要重新计算购物车中所有商品的总价,并将其显示在页面上。
下面是一个简单的示例代码,可以帮助你实现一个基本的 JS 购物车功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS 购物车</title>
</head>
<body>
<h1>商品列表</h1>
<ul>
<li data-name="iPhone" data-price="5999">iPhone - 5999元 <button class="add-to-cart">添加到购物车</button></li>
<li data-name="iPad" data-price="3999">iPad - 3999元 <button class="add-to-cart">添加到购物车</button></li>
<li data-name="Macbook" data-price="9999">Macbook - 9999元 <button class="add-to-cart">添加到购物车</button></li>
</ul>
<h1>购物车</h1>
<table>
<thead>
<tr>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>总价</th>
<th>操作</th>
</tr>
</thead>
<tbody id="cart"></tbody>
<tfoot>
<tr>
<td colspan="3">总价</td>
<td id="total-price">0</td>
<td></td>
</tr>
</tfoot>
</table>
<script>
// 商品列表
const products = [
{
name: 'iPhone',
price: 5999
},
{
name: 'iPad',
price: 3999
},
{
name: 'Macbook',
price: 9999
}
];
// 购物车
let cart = [];
// 添加到购物车按钮
const addToCartButtons = document.querySelectorAll('.add-to-cart');
addToCartButtons.forEach(button => {
button.addEventListener('click', event => {
const product = event.target.parentNode;
const name = product.dataset.name;
const price = product.dataset.price;
addToCart(name, price);
});
});
// 添加到购物车函数
function addToCart(name, price) {
// 查找购物车中是否已有该商品
const index = cart.findIndex(item => item.name === name);
if (index !== -1) {
// 如果已有该商品,数量加 1
cart[index].quantity++;
} else {
// 如果没有该商品,添加到购物车
cart.push({
name,
price,
quantity: 1
});
}
// 更新购物车界面
renderCart();
}
// 删除购物车中的商品
function removeFromCart(index) {
cart.splice(index, 1);
// 更新购物车界面
renderCart();
}
// 渲染购物车界面
function renderCart() {
const cartTable = document.querySelector('#cart');
const total = document.querySelector('#total-price');
// 清空购物车表格
cartTable.innerHTML = '';
// 遍历购物车数组,渲染每个商品行
cart.forEach((item, index) => {
const row = document.createElement('tr');
// 商品名称
const nameCell = document.createElement('td');
nameCell.textContent = item.name;
row.appendChild(nameCell);
// 商品单价
const priceCell = document.createElement('td');
priceCell.textContent = item.price;
row.appendChild(priceCell);
// 商品数量
const quantityCell = document.createElement('td');
quantityCell.textContent = item.quantity;
row.appendChild(quantityCell);
// 商品总价
const totalCell = document.createElement('td');
totalCell.textContent = item.price * item.quantity;
row.appendChild(totalCell);
// 操作按钮
const buttonCell = document.createElement('td');
const removeButton = document.createElement('button');
removeButton.textContent = '删除';
removeButton.addEventListener('click', event => {
removeFromCart(index);
});
buttonCell.appendChild(removeButton);
row.appendChild(buttonCell);
cartTable.appendChild(row);
});
// 计算总价
const totalPrice = cart.reduce((acc, item) => {
return acc + item.price * item.quantity;
}, 0);
total.textContent = totalPrice;
}
</script>
</body>
</html>
```
这段代码包括一个商品列表和一个购物车表格。当用户点击“添加到购物车”按钮时,商品信息将被添加到购物车数组中,并且购物车表格将被更新以显示所有已添加的商品。用户还可以单击“删除”按钮从购物车中删除商品。在购物车表格底部,总价将自动更新以显示所有商品的总价。