<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
thead tr{
height: 50px;
background-color: #00ff00;
}
td{
width: 142px;
height: 26px;
text-align: center;
}
button{
padding: 0 6px;
}
#template{
display: none;
}
</style>
</head>
<body>
<table cellspacing="0" width="700px" border="1" align="center">
<thead>
<tr>
<th>商品名称</th>
<th>数量</th>
<th>单价</th>
<th>小计</th>
<th>操作</th>
</tr>
<tbody class="shopcartbody">
<tr id="template">
<td class="subname"></td>
<td>
<button class="sub">-</button>
<span class="count">0</span>
<button class="add">+</button>
</td>
<td>
单价:¥
<span class="subprice"></span>
</td>
<td>
小计:
<span class="subtotal"></span>
</td>
<td>
操作
<button class="detel">删除</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5">
商品一共<span class="counts">0</span>件,花费一共¥<span class="money">0</span>;
</td>
</tr>
</tfoot>
</thead>
</table>
<script>
let cart = {
json:[
{
name:"锅包肉",
price:40,
},
{
name:"铁锅炖大鹅",
price:100,
},
{
name:"猪肉炖粉条",
price:60,
},
{
name:"烤乳猪",
price:200,
},
{
name:"蒸羊羔",
price:100,
},
{
name:"江米酿鸭子",
price:150,
}
],
template:document.querySelector("#template"),
tbody:document.querySelector(".shopcartbody"),
addNode:"",
subNode:"",
delNode:"",
subtotal:"",
countNodess:document.querySelector(".counts"),
money:document.querySelector(".money"),
countNode:"",
resultcount:0,
resultmoney:0,
init:function(){
this.json.forEach((item)=>{
let netItem = this.template.cloneNode(true);
netItem.id = "";
this.tbody.insertBefore(netItem,this.template);
netItem.querySelector(".subname").innerHTML = item.name;
netItem.querySelector(".subprice").innerHTML = item.price;
this.addNode = document.querySelectorAll(".add");
this.subNode = document.querySelectorAll(".sub");
this.delNode = document.querySelectorAll(".detel");
this.subtotal = document.querySelectorAll(".subtotal");
this.countNode = document.querySelectorAll(".count");
})
this.bindEvent();
},
computedTotal:function(){
this.subtotal = document.querySelectorAll(".subtotal");
this.countNode = document.querySelectorAll(".count");
this.resultcount = 0;
this.resultmoney = 0;
for(let i = 0; i < this.countNode.length; i++){
this.resultcount += Number(this.countNode[i].innerHTML);
this.resultmoney += Number(this.subtotal[i].innerHTML);
}
this.countNodess.innerHTML = this.resultcount;
this.money.innerHTML = this.resultmoney;
},
bindEvent:function(){
for(let i = 0; i < this.addNode.length; i++){
this.addNode[i].onclick = function(){
this.parentNode.querySelector(".count").innerHTML = parseInt(this.parentNode.querySelector(".count").innerHTML) + 1;
this.parentNode.parentNode.querySelector(".subtotal").innerHTML = this.parentNode.querySelector(".count").innerHTML * this.parentNode.parentNode.querySelector(".subprice").innerHTML;
cart.computedTotal();
};
this.subNode[i].onclick = function(){
if (Number(this.parentNode.querySelector(".count").innerHTML) === 0) {
return;
}
this.parentNode.querySelector(".count").innerHTML = parseInt(this.parentNode.querySelector(".count").innerHTML - 1);
this.parentNode.parentNode.querySelector(".subtotal").innerHTML = this.parentNode.querySelector(".count").innerHTML * this.parentNode.parentNode.querySelector(".subprice").innerHTML;
cart.computedTotal();
};
this.delNode[i].onclick = function(){
let nowTr = this.parentNode.parentNode;
nowTr.style.opacity = 1;
let o = setInterval(function(){
let ol = nowTr.style.opacity;
if(ol <= 0){
clearInterval(o);
nowTr.style.opacity = 0;
nowTr.style.display = "none";
nowTr.parentNode.removeChild(nowTr);
cart.computedTotal();
return;
}
nowTr.style.opacity = ol - 0.03;
},16)
}
}
},
}
cart.init();
</script>
````