参照购物车HTML代码,依次完成购物车的商品全选、增减商品数量、修改商品小计、计算商品总额、删除商品、选中商品添加背景 等功能,
时间: 2023-12-28 19:20:54 浏览: 128
以下是购物车HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>购物车</title>
<style type="text/css">
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 5px;
text-align: center;
}
.check input[type="checkbox"] {
margin-right: 5px;
}
.total {
font-weight: bold;
text-align: right;
padding-right: 5px;
}
.selected {
background-color: yellow;
}
</style>
</head>
<body>
<h1>购物车</h1>
<table>
<thead>
<tr>
<th class="check"><input type="checkbox" id="check-all"></th>
<th>商品名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td class="check"><input type="checkbox" class="check-item"></td>
<td>商品1</td>
<td>10</td>
<td><button class="btn-minus">-</button> <span class="quantity">1</span> <button class="btn-plus">+</button></td>
<td class="subtotal">10</td>
<td><button class="btn-delete">删除</button></td>
</tr>
<tr>
<td class="check"><input type="checkbox" class="check-item"></td>
<td>商品2</td>
<td>20</td>
<td><button class="btn-minus">-</button> <span class="quantity">1</span> <button class="btn-plus">+</button></td>
<td class="subtotal">20</td>
<td><button class="btn-delete">删除</button></td>
</tr>
<tr>
<td class="check"><input type="checkbox" class="check-item"></td>
<td>商品3</td>
<td>30</td>
<td><button class="btn-minus">-</button> <span class="quantity">1</span> <button class="btn-plus">+</button></td>
<td class="subtotal">30</td>
<td><button class="btn-delete">删除</button></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4" class="total">商品总额:</td>
<td id="total">0</td>
<td></td>
</tr>
</tfoot>
</table>
<script type="text/javascript">
// 商品全选
var checkAll = document.getElementById("check-all");
var checkItems = document.getElementsByClassName("check-item");
checkAll.addEventListener("click", function() {
for (var i = 0; i < checkItems.length; i++) {
checkItems[i].checked = checkAll.checked;
updateSelected();
}
});
for (var i = 0; i < checkItems.length; i++) {
checkItems[i].addEventListener("click", function() {
updateSelected();
});
}
// 增减商品数量
var btnMinus = document.getElementsByClassName("btn-minus");
var btnPlus = document.getElementsByClassName("btn-plus");
var quantity = document.getElementsByClassName("quantity");
var subtotal = document.getElementsByClassName("subtotal");
for (var i = 0; i < btnMinus.length; i++) {
btnMinus[i].addEventListener("click", function() {
var q = parseInt(this.nextElementSibling.innerHTML);
if (q > 1) {
q--;
this.nextElementSibling.innerHTML = q;
var p = parseInt(this.parentNode.previousElementSibling.innerHTML);
this.parentNode.nextElementSibling.innerHTML = p * q;
updateTotal();
}
});
}
for (var i = 0; i < btnPlus.length; i++) {
btnPlus[i].addEventListener("click", function() {
var q = parseInt(this.previousElementSibling.innerHTML);
q++;
this.previousElementSibling.innerHTML = q;
var p = parseInt(this.parentNode.previousElementSibling.innerHTML);
this.parentNode.nextElementSibling.innerHTML = p * q;
updateTotal();
});
}
// 修改商品小计
for (var i = 0; i < subtotal.length; i++) {
subtotal[i].addEventListener("input", function() {
if (isNaN(parseInt(this.innerHTML))) {
this.innerHTML = 0;
}
updateTotal();
});
}
// 计算商品总额
function updateTotal() {
var total = 0;
for (var i = 0
阅读全文