效果图:
实现功能:
1、点击加减按钮需要修改商品的 单个商品的金额,以及总计金额
2、删除按钮,需要将当前商品整行移除,可能需要修改总金额(勾选上)
3、当勾选商品之后,需要计算总金额
4、全选按钮,需要计算总金额
代码实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物车</title>
<link rel="stylesheet" type="text/css" href="cart.css">
</head>
<body>
<div id="app">
<div class="shoppingBar">
<h2>购物清单</h2>
<!-- 购物车商品 -->
<table>
<tbody>
<tr>
<th><input type="checkbox" title="全选" class="selectAll"></th>
<th>商品名称</th>
<th>数量</th>
<th>单价(元)</th>
<th>金额(元)</th>
<th>操作</th>
</tr>
<!-- 电视机 -->
<tr class="commodity">
<th>
<input type="checkbox" class="input" value="1">
</th>
<th class="content">
<img src="img/TV.jpeg" width="100px" height="100px">
<p>电视机 </p>
</th>
<th>
<button type="button" class="decr">
-
</button>
<span>
1
</span>
<button type="button" class="incr">
+
</button>
</th>
<th>
200
</th>
<th class="money">
200
</th>
<th>
<span class="glyphicon glyphicon-remove">删除</span>
</th>
</tr>
<!-- 洗衣机 -->
<tr class="commodity">
<th>
<input type="checkbox" class="input" value="2">
</th>
<th class="content">
<img src="img/xiyiji.jpeg" width="100px" height="100px">
<p>洗衣机 </p>
</th>
<th>
<button type="button" class="decr">
-
</button>
<span>
1
</span>
<button type="button" class="incr">
+
</button>
</th>
<th>
150
</th>
<th class="money">
150
</th>
<th>
<span class="glyphicon glyphicon-remove">删除</span>
</th>
</tr>
<!-- 冰箱 -->
<tr class="commodity">
<th>
<input type="checkbox" class="input" value="3">
</th>
<th class="content">
<img src="img/bingx.jpeg" width="100px" height="100px">
<p>冰箱</p>
</th>
<th>
<button type="button" class="decr">
-
</button>
<span>
1
</span>
<button type="button" class="incr">
+
</button>
</th>
<th>
100
</th>
<th class="money">
100
</th>
<th>
<span class="glyphicon glyphicon-remove">删除</span>
</th>
</tr>
</tbody>
</table>
<!-- 底部金额 -->
<div class="shoppingBar-footer">
<div class="manage">
<span class="delAll">
删除所选商品
</span>
<span class="return">
继续购物
</span>
</div>
<button id="go" type="button">
去结算
</button>
<div class="buy">商品总计:
<span>
¥0
</span>
</div>
</div>
</div>
</div>
</body>
<!-- <button id="test">测试按钮</button> -->
</html>
<script>
// 测试按钮
// document.querySelector('#test').onclick = function () {
// getTotal();
// }
// 计算勾选商品的总金额
function getTotal() {
// 存放总金额
var total = 0;
// 找到被勾选商品计算总金额
document.querySelectorAll('.input:checked').forEach(function (dom) {
console.log(dom);//每一个被勾选的input标签
// 找到被勾选商品的单价---input标签父元素的兄弟元素
total += parseFloat(dom.parentElement.parentElement.querySelector('.money').innerHTML.trim())
})
// 把勾选商品的单价同步到底部总金额
document.querySelector('.buy>span').innerHTML = '¥' + total;
}
// 实现删除功能
document.querySelectorAll('.glyphicon-remove').forEach(function (dom) {
// 给每一个删除按钮绑定点击事件
dom.onclick = function () {
// 删除整行
document.querySelector('tbody').removeChild(this.parentElement.parentElement);
// 更新总金额
getTotal();
// 判断商品是否全部删除,全部删除则去掉全选
document.querySelector('.selectAll').checked = (document.querySelectorAll('.input').length == document.querySelectorAll('.input:checked').length);
if (document.querySelectorAll('.input').length == 0) {
document.querySelector('.selectAll').checked = false;
}
}
})
// 实现数量的增加
document.querySelectorAll('.incr').forEach(function (dom) {
// 给每一个加号绑定点击事件
dom.onclick = function () {
console.log(this);//button标签
// 修改数量 --- 找到button上一个元素(数量) previousElementSibling
var count = this.previousElementSibling.innerHTML.trim() - 0 + 1;
this.previousElementSibling.innerHTML = count;
// 获取点击商品的单价 -- 找到存放单价的span标签
var price = this.parentElement.nextElementSibling.innerHTML.trim();
// 计算点击商品的总价 -- 找到存放总价的th标签
this.parentElement.nextElementSibling.nextElementSibling.innerHTML = count * parseFloat(price);
getTotal();
}
})
// 实现数量的减少
document.querySelectorAll('.decr').forEach(function (dom) {
// 给每一个减号绑定点击事件
dom.onclick = function () {
// 修改数量 --- 先获取数量
var count = this.nextElementSibling.innerHTML.trim() - 0;
if (count <= 1) {
alert('不能再减啦!');
return;
}
this.nextElementSibling.innerHTML = count - 1;
// 获取点击商品的单价 -- 找到存放单价的span标签
var price = this.parentElement.nextElementSibling.innerHTML.trim();
// 计算点击商品的总价 -- 找到存放总价的th标签
this.parentElement.nextElementSibling.nextElementSibling.innerHTML = count * parseFloat(price);
getTotal();
}
})
// 实现全选功能
document.querySelector('.selectAll').onclick = function () {
// 保存全选按钮的勾选状态,得到是一个布尔值
var selectAllChecked = this.checked;
document.querySelectorAll('.input').forEach(function (dom) {
// 把全选的状态赋值给单选按钮
dom.checked = selectAllChecked;
})
getTotal();
}
// 给单选框绑定点击事件 ---- arr input数组
document.querySelectorAll('.input').forEach(function (dom, index, arr) {
dom.onclick = function () {
// console.log(arr);
// 实现单选商品添加总价
getTotal();
// 判断单选按钮是否全部勾选上了
// document.querySelector('.selectAll').checked = (arr.length == document.querySelectorAll('.input:checked').length);
// 上下等同
document.querySelector('.selectAll').checked = (document.querySelectorAll('.input').length == document.querySelectorAll('.input:checked').length);
}
})
</script>
body{
margin: 0;
padding: 0;
height: auto;
text-decoration: none;
list-style: none;
font-size: 12px;
background-color: #F5F5F5;
}
.shoppingBar {
width: 1130px;
border: 2px solid #F5F5F5;
margin: 40px auto;
position: relative;
background-color: white;
font-size: 16px;
}
.shoppingBar h2 {
height: 60px;
line-height: 60px;
color: black;
text-indent: 40px;
}
.shoppingBar table {
width: 100%;
margin-bottom: 60px;
}
.shoppingBar table .money{
color: red;
}
.shoppingBar table .commodity{
border: 3px solid #F5F5F5;
height: 150px;
}
.shoppingBar>table tr th input{
width: 25px;
height: 25px;
margin-left: 30px;
}
.shoppingBar table tr:first-of-type {
height: 80px;
color: #424242;
font-size: 14px;
border-top: 1px solid #F5F5F5;
border-bottom: 1px solid #F5F5F5;
}
.shoppingBar table tr th img {
width: 120px;
height: 100px;
float: left;
}
.shoppingBar table tr th p {
float: right;
width: 100px;
font-size: 18px;
color: #424242;
}
.shoppingBar table tr .content {
width: 220px;
padding: 0 100px;
}
.shoppingBar table tr #delete {
cursor: pointer;
font-size: 20px;
}
.shoppingBar table tr #delete:hover{
color: red;
}
.shoppingBar .shoppingBar-footer {
border: 1px solid #F5F5F5;
width: 1130px;
height: 60px;
position: absolute;
margin-top: 10px;
color: #757575;
font-size: 14px;
background-color: white;
}
.shoppingBar .shoppingBar-footer .manage {
float: left;
height: 40px;
}
.shoppingBar .shoppingBar-footer .manage .delAll,
.return {
cursor: pointer;
background: #F5F5F5;
color: #757575;
line-height: 60px;
margin: 0px 30px;
}
.shoppingBar .shoppingBar-footer .manage .delAll,
.return:hover{
color: #FF6700;
transition: all 0.5s ease-in-out;
}
.shoppingBar .shoppingBar-footer .buy {
float: right;
height: 40px;
line-height: 60px;
}
.shoppingBar .shoppingBar-footer #go {
float: right;
height: 60px;
width: 110px;
font-weight: 400;
font-size: 18px;
outline: none;
background-color: #E0E0E0;
border: none;
border-radius: 2px;
margin-left: 10px;
cursor: pointer;
color: white;
}
.shoppingBar .shoppingBar-footer .active{
background-color: #FF6700;
}
.shoppingBar .shoppingBar-footer .buy span {
font-weight: bold;
font-size: 30px;
color: red;
}
.shoppingBar .shoppingBar-footer #go:hover{
background-color: red;
transition: all 0.5s ease 0s;
}