HTML代码
<div class="box3">
<div class="box3-top">
<table>
<tr class="tr1">
<td>
<input type="checkbox" id="checkall">
<label for="checkall">全选</label>
</td>
<td>商品信息</td>
<td>规格</td>
<td>单价(元)</td>
<td>数量</td>
<td>小计(元)</td>
<td>操作</td>
</tr>
<tr class="tr"></tr>
<tr class="tr2">
<td class="td1"><input type="checkbox" id="checkall" class="checkone">
</td>
<td class="td2"><img class="left" src="img/list1.png" alt="">
<p class="left"><a href="">苹果(Apple) iPhone 13 256GB 星光色 移动联通电信5G全网通手机 双卡双待 苹果iphone13
</a></p>
</td>
<td class="td3">
<p>颜色:星光色</p>
<p>版本:iPhone 13 (256GB)</p>
</td>
<td class="td4">
<span>$6159.00</span>
</td>
<td class="td5">
<div class="num">
<span class="jian left">-</span>
<span class="num1">1</span>
<span class="jia right">+</span>
</div>
</td>
<td class="td6">$6159.00</td>
<td class="td7">
<p>移入关注</p>
<p class="del">删除</p>
</td>
</tr>
<tr class="tr2">
<td class="td1"><input type="checkbox" id="checkall" class="checkone">
</td>
<td class="td2"><img class="left" src="img/list2.png" alt="">
<p class="left"><a href=""> [苏宁官旗]3M厨下式家用直饮净水器<br> 净滋CDW7101V母婴型净水直饮机 无桶无废水
</a></p>
</td>
<td class="td3">
<p>型号:7101型母婴全效款</p>
<p></p>
</td>
<td class="td4">
<span>$1249.00</span>
</td>
<td class="td5">
<div class="num">
<span class="jian">-</span>
<span class="num1">1</span>
<span class="jia">+</span>
</div>
</td>
<td class="td6">$1249.00</td>
<td class="td7">
<p>移入关注</p>
<p class="del">删除</p>
</td>
</tr>
<tr class="tr"></tr>
<tr class="tr2">
<td class="td1"><input type="checkbox" id="checkall" class="checkone">
</td>
<td class="td2"><img class="left" src="img/list3.png" alt="">
<p class="left"><a href="">
苏泊尔(SUPOR)电饭煲5升电饭锅 迷你触屏智能IH电磁精铁球釜内胆高端家用煮饭锅黑色5L
</a></p>
</td>
<td class="td3">
<p>容量:专柜SF50HC32</p>
<p></p>
</td>
<td class="td4">
<span>$799.00</span>
</td>
<td class="td5">
<div class="num">
<span class="jian">-</span>
<span class="num1">1</span>
<span class="jia">+</span>
</div>
</td>
<td class="td6">$799.00</td>
<td class="td7">
<p>移入关注</p>
<p class="del">删除</p>
</td>
</tr>
<tr class="tr2">
<td class="td1"><input type="checkbox" id="checkall" class="checkone">
</td>
<td class="td2"><img class="left" src="img/list4.png" alt="">
<p class="left"><a href="">百雀羚草本水嫩精纯明星美肌水100ml 补水保湿滋润肌肤淡化细纹长效锁水 </a></p>
</td>
<td class="td3">
<p>水嫩精纯:明星美肌</p>
<p></p>
</td>
<td class="td4">
<span>$79.00</span>
</td>
<td class="td5">
<div class="num">
<span class="jian">-</span>
<span class="num1">1</span>
<span class="jia">+</span>
</div>
</td>
<td class="td6">$79.00</td>
<td class="td7">
<p>移入关注</p>
<p class="del">删除</p>
</td>
</tr>
<tr class="tr"></tr>
<tr class="tr"></tr>
</table>
<table>
<tr class="tr3">
<td class="jieshuan">
<div class="quanxuan left">
<input type="checkbox" id="checkall1">
<label for="checkall1">全选</label>
<span id="del1">删除选中商品</span>
</div>
</td>
<td>
<div class="jiage left">
<p>总价(含运费):
<span class="sum">¥0.00</span>
</p>
</div>
</td>
<td>
<button class="right">去结算</button>
</td>
</tr>
</table>
</div>
</div>
css代码
body .box3 {
color: #666;
}
body .box3 table {
width: 1190px;
font-size: 12px;
}
body .box3 table .tr {
height: 30px;
}
body .box3 table .tr1 {
width: 1190px;
height: 32px;
line-height: 32px;
border: 1px solid #ddd;
border-top: 3px solid #ddd;
}
body .box3 table .tr2 {
height: 134px;
border: 1px solid #ddd;
}
body .box3 table .tr2 img {
width: 80px;
margin-right: 10px;
}
body .box3 table .tr2 a {
color: #333;
}
body .box3 table .tr2 a:hover {
color: #f70;
}
body .box3 table .tr2 td {
vertical-align: top;
padding-top: 15px;
}
body .box3 table .tr2 .td1 {
width: 120px;
}
body .box3 table .tr2 .td2 {
width: 400px;
}
body .box3 table .tr2 .td2 p {
width: 240px;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
body .box3 table .tr2 .td3 {
width: 180px;
}
body .box3 table .tr2 .td3 p {
color: #999;
width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
body .box3 table .tr2 .td4 {
width: 130px;
color: #333;
}
body .box3 table .tr2 .td5 {
width: 140px;
}
body .box3 table .tr2 .td5 .num {
display: flex;
width: 80px;
height: 22px;
line-height: 22px;
justify-content: space-evenly;
border: 1px solid #ddd;
text-align: center;
}
body .box3 table .tr2 .td5 .num span {
width: 30px;
line-height: 20px;
cursor: pointer;
}
body .box3 table .tr2 .td5 .num .jian,
body .box3 table .tr2 .td5 .num .jia {
line-height: 18px;
background-color: #ccc;
}
body .box3 table .tr2 .td6 {
width: 130px;
color: #333;
font-weight: 600;
}
body .box3 table .tr2 .td7 {
color: #666;
}
body .box3 table .tr2 .td7 p {
line-height: 20px;
cursor: pointer;
}
body .box3 table .tr2 .td7 p:hover {
color: #f70;
}
body .box3 table .tr3 {
height: 60px;
width: 1190px;
border: 1px solid #ddd;
}
body .box3 table .jieshuan {
line-height: 60px;
}
body .box3 table .jieshuan span {
margin-left: 50px;
cursor: pointer;
}
body .box3 table .jieshuan input {
vertical-align: middle;
}
body .box3 table .jiage {
line-height: 56px;
flex-wrap: wrap;
font-size: 14px;
color: #333;
}
body .box3 table .jiage .sum {
font-size: 22px;
color: #f60;
}
body .box3 table button {
height: 60px;
width: 120px;
font-weight: 700;
font-size: 18px;
border: none;
color: #fff;
background-color: #f60;
cursor: pointer;
}
body .box3 table button:hover {
background-color: #f50;
}
js代码
var ckAll = document.getElementById("checkall");
var ckAll1 = document.getElementById("checkall1");
var ckOnes = document.querySelectorAll(".checkone");
var _jian = document.querySelectorAll('.jian')
var _jia = document.querySelectorAll('.jia');
var _num1 = document.querySelectorAll('.num1');
var _td6 = document.querySelectorAll('.td6');
var _banjia = document.querySelectorAll('.td4 span')
var _del1 = document.getElementById('del1');
//选择效果
ckAll.onchange = function () {
for (var i = 0; i < ckOnes.length; i++) {
ckOnes[i].checked = this.checked;
}
ckAll1.checked = ckAll.checked;
getSum();
checkPro();
}
ckAll1.onchange = function () {
for (var i = 0; i < ckOnes.length; i++) {
ckOnes[i].checked = this.checked;
}
ckAll.checked = ckAll1.checked;
getSum();
}
ckOnes.forEach(item => {
item.onchange = function () {
checkPro();
getSum();
}
})
function checkPro() {
ckOnes = document.querySelectorAll(".checkone");
if (ckOnes.length === 0) {
ckAll.checked = false;
return;
}
ckAll.checked = ckAll1.checked = Array.from(ckOnes).every(item => item.checked === true);
console.log(ckOnes);
}
//商品数量更改及小计
for (let i = 0; i < _jia.length; i++) {
_jia[i].onclick = function () {
let j = parseInt(_num1[i].innerHTML)
j += 1;
let n = _banjia[i].innerHTML.replace(/[^0-9.0-9]/ig, "")
_num1[i].innerHTML = j;
_td6[i].innerHTML = '$' + (j * n).toFixed(2)
getSum();
}
}
for (let i = 0; i < _jian.length; i++) {
let n = _banjia[i].innerHTML.replace(/[^0-9.0-9]/ig, "")
_jian[i].onclick = function () {
let j = _num1[i].innerHTML
if (j == 1) {
alert('商品数量至少为1件')
} else {
j -= 1;
_num1[i].innerHTML = j;
_td6[i].innerHTML = '$' + (j * n).toFixed(2)
}
getSum();
}
}
//删除
var tab = document.querySelector('table');
var _tr2 = document.querySelectorAll('.tr2')
var _del = document.querySelectorAll('.del')
for (let i = 0; i < _tr2.length; i++) {
_del[i].onclick = function () {
let f = confirm('是否删除该商品')
if (f == true) {
_tr2[i].remove()
}
getSum();
checkPro();
}
}
// 删除选中
_del1.onclick = function () {
let f = confirm('是否删除该商品')
if (f == true) {
ckOnes.forEach(item => {
if (item.checked) {
item.parentElement.parentElement.remove();
ckAll1.checked = false;
}
});
}
getSum();
checkPro();
}
//结算
var _sum = document.querySelector('.sum')
function getSum() {
ckOnes = document.querySelectorAll(".checkone");
var s = 0, t = 0;
ckOnes.forEach((item, i) => {//遍历所有单个商品的复选框
if (item.checked) { //如果单个商品的复选框是选中状态
s += +item.parentElement.parentElement.getElementsByClassName("td6")[0].innerText.replace(/[^0-9.0-9]/ig, ""); //单个商品的总价进行叠加求和
}
});
_sum.innerText = '$' + s.toFixed(2);
}
getSum()
效果图