<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物页面</title>
<style>
ul{list-style:none;padding:0;margin:0;}
.goods li{display:inline-block;border:1px solid #ddd;padding:10px;margin:10px;}
.goods li:hover{background-color:#efefef;}
.goods .price{color:#f00;font-weight:bold;}
.goods .price::before{
content:"¥";
}
</style>
<script>
window.onload = function(){
var goods = document.getElementsByClassName('goods')[0];
// 用于保存购物车商品信息
var carList = [];
// 先获取当前cookie
var cookies = document.cookie.split('; ');
for(var i=0;i<cookies.length;i++){
var arr = cookies[i].split('=');
if(arr[0] === 'carlist'){
carList = JSON.parse(arr[1]);
}
}
// 事件委托
goods.onclick = function(e){
e = e || window.event;
var target = e.target || e.srcElement;
// 添加到购物车
if(target.tagName.toLowerCase() === 'button'){
// 获取当前li
var currentLi = target.parentElement.parentElement;
var children = currentLi.children;
var currentGUID = currentLi.getAttribute('data-guid');
// 先创建一个对象保存当前商品信息
var goodsObj = {};
goodsObj.guid = currentGUID;
goodsObj.qty = 1;
goodsObj.name = children[1].innerHTML;
goodsObj.price = children[2].innerHTML;
goodsObj.imgUrl = children[0].src;
// 如果cookie为空,则直接添加
if(carList.length===0){
// 添加到carList
carList.push(goodsObj);
}else{
// 先判断cookie中有无相同的guid商品
for(var i=0;i<carList.length;i++){
// 如果商品已经存在cookie中,则数量+1
if(carList[i].guid === currentGUID){
carList[i].qty++;
break;
}
}
// 如果原cookie中没有当前商品
if(i===carList.length){
// 添加到carList
carList.push(goodsObj);
}
}
// 存入cookie
// 把对象/数组转换诚json字符串:JSON.stringify()
document.cookie = 'carlist=' + JSON.stringify(carList);
}
}
}
</script>
</head>
<body>
<ul class="goods">
<li data-guid="g01">
<img src="images/shirt_1.jpg">
<p>短袖衬衣</p>
<p class="price">98.88</p>
<div class="add2cart">
<button>添加到购物车</button>
</div>
</li>
<li data-guid="g02">
<img src="images/shirt_2.jpg">
<p>短袖衬衣2</p>
<p class="price">88.88</p>
<div class="add2cart">
<button>添加到购物车</button>
</div>
</li>
<li data-guid="g03">
<img src="images/shirt_3.jpg">
<p>短袖衬衣3</p>
<p class="price">9.98</p>
<div class="add2cart">
<button>添加到购物车</button>
</div>
</li>
<li data-guid="g04">
<img src="images/shirt_4.jpg">
<p>短袖衬衣4</p>
<p class="price">8.88</p>
<div class="add2cart">
<button>添加到购物车</button>
</div>
</li>
</ul>
<a href="04car.html">去结算</a>
</body>
</html>
//购物车页面、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物车</title>
<style>
#carList li{position:relative;padding-bottom:15px;margin-bottom:15px;border-bottom:1px solid #ddd;}
#carList img{display:block;100px;}
#carList li .btn-close{position:absolute;top:0;right:0;padding:0 5px;cursor:default;}
#carList li .btn-close:hover{background-color:#f00;color:#fff;}
.subPrice{padding:5px 20px;color:#f00;font-weight:bold;text-align:right;}
#carList .price{color:#f00;}
.price::before{
content:'¥';
font-size:11px;
}
#carList .price span{font-size:11px;}
</style>
<script>
window.onload = function(){
/*
读取cookie中的carlist
把json字符串转换成对象/数组:JSON.parse()
json字符串格式:
1.必须用双引号
2.不能右注释
*/
var oCarList = document.getElementById('carList');
var oSubPrice = oCarList.nextElementSibling;
var btnClear = document.getElementById('btnClear');
var carList;
var cookies = document.cookie.split('; ');
for(var i=0;i<cookies.length;i++){
var arr = cookies[i].split('=');
if(arr[0] === 'carlist'){
console.log(JSON.parse(arr[1]));
carList = JSON.parse(arr[1]);
}
}
var subPrice = 0;
if(carList){
var ul = document.createElement('ul');
for(var i=0;i<carList.length;i++){
var li = document.createElement('li');
// 给每个li添加data-guid属性
li.setAttribute('data-guid',carList[i].guid);
// 商品名
var title = document.createElement('h4');
title.innerHTML = carList[i].name;
// 商品价格
var price = document.createElement('p');
price.className = 'price';
price.innerHTML = carList[i].price + '×' + carList[i].qty;
// 商品图片
var img = document.createElement('img');
img.src = carList[i].imgUrl;
// 添加删除按钮
var btnClose = document.createElement('span');
btnClose.innerHTML = '×';
btnClose.className = 'btn-close';
// 计算总价
subPrice += carList[i].price*carList[i].qty;
li.appendChild(title);
li.appendChild(price);
li.appendChild(img);
li.appendChild(btnClose);
ul.appendChild(li);
}
// 写入页面
oCarList.appendChild(ul);
// 写入总价
// toFixed(n)获取小数点后n位(自动四舍五入,Number类型的方法)
oSubPrice.innerHTML = '<span class="price">' + subPrice.toFixed(2) + '</span>';
}
// 删除商品
oCarList.onclick = function(e){
e = e || window.event;
var target = e.target || e.srcElement;
// 是否点击了删除按钮
if(target.className === 'btn-close'){
var currentLi = target.parentElement;
// 获取当前guid
var currentGUID = currentLi.getAttribute('data-guid');
// 删除cookie中对应的商品
// 根据guid取对比
for(var i=0;i<carList.length;i++){
// 找出要删除的商品
if(carList[i].guid === currentGUID){
carList.splice(i,1);
break;
}
}
// 更新cookie
document.cookie = 'carlist=' + JSON.stringify(carList);
// 删除li节点
currentLi.parentElement.removeChild(currentLi);
}
}
// 清空购物车
// 1、删除DOM节点
// 2、删除cookie
btnClear.onclick = function(){
oCarList.innerHTML = '';
oSubPrice.innerHTML = '';
// 利用设置有效期位过期事件来达到删除cookie的效果
var now = new Date();
now.setDate(now.getDate()-7);
document.cookie = 'carlist=xx;expires=' + now;
}
}
</script>
</head>
<body>
<h1>购物车</h1>
<div id="carList">
</div>
<div class="subPrice"></div>
<a href="#" id="btnClear">清空购物车</a>
</body>
</html>
本文实例讲述了jquery.cookie实现的客户端购物车操作。分享给大家供大家参考,具体如下:
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
|
//购物车 var Cart = function () { this .Count = 0; this .Total = 0; this .Items = new Array(); }; //购物车集合对象 var CartItem = function () { this .Id = 0; this .Name = "" ; this .Count = 0; this .Price = 0; }; //购物车操作 var CartHelper = function () { this .cookieName = "yxhCart" ; this .Clear = function () { var cart = new Cart(); this .Save(cart); return cart; }; //向购物车添加 this .Add = function (id, name, count, price) { var cart = this .Read(); var index = this .Find(id); //如果ID已存在,覆盖数量 if (index > -1) { cart.Total -= (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); cart.Items[index].Count = count; cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); } else { var item = new CartItem(); item.Id = id; item.Name = name; item.Count = count; item.Price = price; cart.Items.push(item); cart.Count++; cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); } this .Save(cart); return cart; }; //改变数量 this .Change = function (id, count) { var cart = this .Read(); var index = this .Find(id); cart.Items[index].Count = count; this .Save(cart); return cart; }; //移出购物车 this .Del = function (id) { var cart = this .Read(); var index = this .Find(id); if (index > -1) { var item = cart.Items[index]; cart.Count--; cart.Total = cart.Total - (((item.Count * 100) * (item.Price * 100)) / 10000); cart.Items.splice(index, 1); this .Save(cart); } return cart; }; //根据ID查找 this .Find = function (id) { var cart = this .Read(); var index = -1; for ( var i = 0; i < cart.Items.length; i++) { if (cart.Items[i].Id == id) { index = i; } } return index; }; //COOKIE操作 this .Save = function (cart) { var source = "" ; for ( var i = 0; i < cart.Items.length; i++) { if (source != "" ) { source += "|$|" ; } source += this .ItemToString(cart.Items[i]); } $.cookie( this .cookieName, source); }; this .Read = function () { //读取COOKIE中的集合 var source = $.cookie( this .cookieName); var cart = new Cart(); if (source == null || source == "" ) { return cart; } var arr = source.split( "|$|" ); cart.Count = arr.length; for ( var i = 0; i < arr.length; i++) { var item = this .ItemToObject(arr[i]); cart.Items.push(item); cart.Total += (((item.Count * 100) * (item.Price * 100)) / 10000); } return cart; }; this .ItemToString = function (item) { return item.Id + "||" + escape(item.Name) + "||" + item.Count + "||" + item.Price; }; this .ItemToObject = function (str) { var arr = str.split( '||' ); var item = new CartItem(); item.Id = arr[0]; item.Name = unescape(arr[1]); item.Count = arr[2]; item.Price = arr[3]; return item; }; }; |