购物车小demo

+

实现一个简单的购物车,功能不是特别齐全,还存在一些问题

demo

HTML代码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物车</title>
<link rel="stylesheet" href="index.css">
<script src="data.js"></script>
</head>
<body>
<div class="shopCar">
<div class="control">
<input type="checkbox" value="" id="all">
<label for="all">全选</label>
<input type="button" value="按价格⬆️">
</div>
<div class="list">
<h3>商品</h3>
<h3>购买数量</h3>
<h3>单价</h3>
<h3>小计</h3>
<h3>操作</h3>
</div>
<ul>
<li>
<input type="checkbox">
<h3>摩奥套装</h3>
<div class="shuzhi">
<input class="minus" value="-">
<input type="text" num="" value="1">
<input class="add" value="+">
</div>
<span>¥:<i>99.00</i></span>
<input type="text" class="xiaoji" >
<div class="shanchu">
<a href="javascript:void(0)">删除</a>
</div>
</li>
<li>
<input type="checkbox">
<h3>火锅底料</h3>
<div class="shuzhi">
<input class="minus" value="-">
<input type="text" num="" value="1">
<input class="add" value="+">
</div>
<span>¥:<i>56.00</i></span>
<input type="text" class="xiaoji" >
<div class="shanchu ">
<a href="javascript:void(0)">删除</a>
</div>
</li>
<li>
<input type="checkbox">
<h3>沐浴露</h3>
<div class="shuzhi">
<input class="minus" value="-">
<input type="text" num="" value="1">
<input class="add" value="+">
</div>
<span>¥:<i>74.50</i></span>
<input type="text" class="xiaoji" >
<div class="shanchu ">
<a href="javascript:void(0)">删除</a>
</div>
</li>
<li>
<input type="checkbox">
<h3>坚果</h3>
<div class="shuzhi">
<input class="minus" value="-">
<input type="text" num="" value="1">
<input class="add" value="+">
</div>
<span>¥:<i>23.00</i></span>
<input type="text" class="xiaoji">
<div class="shanchu ">
<a href="javascript:void(0)">删除</a>
</div>
</li>
</ul>
<div class="last">
<label for="allPrice">总计:</label>
<input id="allPrice" type="text" value="0">
</div>
</div>
</body>
<script src="index.js"></script>
</html>

css样式部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
*{
margin:0;
padding:0;
font-size:20px;
}
a{
text-decoration: none;
}
ul{
list-style:none;
}
.shopCar{
margin-left:100px;
}
.control{
height:50px;
margin-bottom:20px;
}
.list{
width:900px;
height:50px;
background:red;
}
.list>h3{
width:165px;
display:inline-block;
text-align:center;
line-height:50px;
}
.list h3:nth-child(1){
margin-left:14px;
}
[type="button"]{
border:none;
height:50px;
width:250px;
border-radius:10px;
outline:none;
margin-left:50px;
}
ul li{
width:900px;
height:70px;
background:snow;
line-height:70px;
border-bottom:1px red solid;
}
li >input{
float:left;
margin-top:30px;
}
li h3{
width:165px;
float:left;
margin-left:20px;
text-align:center;
}
li .shuzhi{
width:165px;
height:70px;
float:left;
text-align:center;
}
li .shuzhi input:nth-child(1){
width:20px;
text-align:center;
background:lightgray;
border:none;
}
li .shuzhi input:nth-child(2){
width:40px;
text-align:center;
}
li .shuzhi input:nth-child(3){
width:20px;
text-align:center;
background:lightgray;
border:none;
}
li >span{
width:166px;
text-align:center;
float:left;
}
li .xiaoji {
width:165px;
float:left;
text-align:center;
outline:none;
border:none;
}
li .shanchu {
width:165px;
float:left;
text-align:center;
}
.last{
margin-left:500px;
margin-top:10px;
}
.last input{
width:150px;
}

js代码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var data = data;
var item = data.item;
function $(x){
return document.querySelector(x);
}
function $s(x){
return document.querySelectorAll(x);
}
var btns=$s("li [type=checkbox]");
var btn=$("#all");
var display=$("#allPrice");
var singlePrice =$s(".xiaoji");
var adds=$s('.add');
var minus=$s('.minus');
var shanchu=$s("li div a");
var Price = 0;
var xuanZhong = true;
var length = btns.length;
// 计算总数
function allPrice(){
Price=0;
for(var i=0;i<length;i++){
if(btns[i].checked){
Price += parseInt($s("[num='']")[i].value) * item[i].price;
}
}
display.value=Price;
}
function xiaoji(){
for(var i=0;i<item.length;i++){
singlePrice[i].value = parseInt($s("[num='']")[i].value) * item[i].price;
}
}
for(var i=0;i<item.length;i++){
singlePrice[i].value = parseInt($s("[num='']")[i].value) * item[i].price;
}
// 点击全选按钮
btn.onclick=function(){
for(var i=0;i<length;i++){
btns[i].checked=this.checked;
}
if(this.checked){
allPrice();
xiaoji();
}
else{
Price=0;
display.value=0;
}
}
// 添加商品数量和减少商品数量
for(var i=0;i<adds.length;i++){
adds[i].index=i;
minus[i].index=i;
adds[i].onclick=function(){
var num=$s("[num='']")[this.index].value;
$s("[num='']")[this.index].value = ++num;
btns[this.index].checked=true;
allPrice();
xiaoji();
panduan();
}
minus[i].onclick=function(){
var num=$s("[num='']")[this.index].value;
if(num <=1){
$s("[num='']")[this.index].value = 1;
}
else{
$s("[num='']")[this.index].value = --num;
btns[this.index].checked=true;
}
allPrice();
xiaoji();
panduan();
}
}
//
for(var i=0;i<length;i++){
btns[i].onclick=function(){
panduan();
}
}
function panduan(){
for(var i=0;i<length;i++){
var isTrue=true;
if(btns[i].checked == false){
btn.checked=false;
}
else if(btns[i].checked == true){
for(var i=0;i<length;i++){
if(btns[i].checked ==false){
isTrue=false;
}
}
if(!isTrue){
btn.checked=false;
}
else{
btn.checked=true;
}
}
allPrice();
xiaoji();
}
}
// 删除商品
for(var i=0;i<length;i++){
shanchu[i].index=i;
shanchu[i].onclick=function(){
$s("li")[this.index].style.display="none";
btns[this.index].checked=false;
allPrice();
}
}
// 价格排序
var button=$("[type=button]");
var ul=$("ul");
var html="";
var isUp=true;
button.onclick=function(){
html="";
if(isUp){
var obj=up();
this.value="按价格⬆️";
for(var i=0;i<obj.length;i++){
html +="<li><input type='checkbox'>"+
"<h3>"+obj[i].title+"</h3>"+
"<div class='shuzhi'>"+
"<input class='minus' value='-'>"+
"<input type='text' num='' value='1'>"+
"<input class='add' value='+'></div>"+
"<span>¥:<i>"+obj[i].price+"</i></span>"+
"<input type='text' class='xiaoji' value='0'>"+
"<div class='shanchu'>"+
"<a href='javascript:void(0)'>"+obj[i].delete+"</a></div></li>"
}
ul.innerHTML=html;
isUp=false;
}
else{
html="";
var obj=down();
this.value="按价格⬇️";
for(var i=0;i<obj.length;i++){
html +="<li><input type='checkbox'>"+
"<h3>"+obj[i].title+"</h3>"+
"<div class='shuzhi'>"+
"<input class='minus' value='-'>"+
"<input type='text' num='' value='1'>"+
"<input class='add' value='+'></div>"+
"<span>¥:<i>"+obj[i].price+"</i></span>"+
"<input type='text' class='xiaoji' value='0'>"+
"<div class='shanchu'>"+
"<a href='javascript:void(0)'>"+obj[i].delete+"</a></div></li>"
}
ul.innerHTML=html;
isUp=true;
}
}
function up(){
var obj = item.sort(function(a,b){
return b.price-a.price;
});
return obj;
}
function down(){
var obj = item.sort(function(a,b){
return a.price-b.price;
});
return obj;
}