Demo—cookie电商购物车
说明:cookie的操作须有域名,简单点说就是需要用发布的方式去访问,查看cookie信息请用开发者模式进入application栏
1.页面布局(结构)(根目录)
商品列表
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品列表</title>
<link rel="stylesheet" type="text/css" href="styles/productList.css"/>
</head>
<body>
<div class="listbox">
<a href="shoppingCart.html" class="mycart">我的购物车</a>
<ul id="list" class="list">
<li>
<a href="javascript:;" class="abtn" data-id="1" data-url="images/1.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/1.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="2" data-url="images/2.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/2.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="3" data-url="images/3.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/3.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="4" data-url="images/4.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/4.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="5" data-url="images/5.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/5.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="6" data-url="images/6.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/6.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="7" data-url="images/7.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/7.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
<li>
<a href="javascript:;" class="abtn" data-id="8" data-url="images/8.jpg" data-title="8h 鹅绒被" data-price="199" data-amount="1">添加到购物车</a>
<img src="images/8.jpg" alt="">
<div class="title">8h 鹅绒被</div>
<div class="price">¥998</div>
</li>
</ul>
</div>
</body>
<script type="text/javascript" src="scripts/common.js"></script>
<script type="text/javascript" src="scripts/productList.js"></script>
</html>
购物车商品页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品列表</title>
<link rel="stylesheet" type="text/css" href="styles/productList.css"/>
</head>
<body>
<div class="listbox">
<a href="productList.html" class="mycart">返回商品列表</a>
<ul id="list" class="list">
</ul>
</div>
</body>
<script type="text/javascript" src="scripts/common.js"></script>
<script type="text/javascript" src="scripts/shoppingCart.js"></script>
</html>
2.页面样式(样式)(文件夹名称:styles)
@charset "ntf-8";
/* css document */
/* 样式覆盖 */
html,body,form,table,th,tr,td,ul,li,ol,dl,dd,dt,p,h1,h2,h3,h4,h5,h6,fieldset,figure,hr,div{
margin: 0;
padding: 0;
}
html,body{
height: 100%;
}
body{
font-family: "Microsoft YaHei","Arial";
font-size: 16px;
background: #f3f3f3;
}
ul,ol{
list-style: none;
}
img{
border: none;
display: block;
}
a{
text-decoration: none;
}
/* listbox */
.listbox{
width: 95%;
margin: 0 auto;
overflow:hidden;
text-align: center;
}
.mycart{
display: inline-block;
padding: 10px 50px;
border: 10px solid #f3f3f3;
font-weight: bold;
font-size: 18px;
color: #000000;
background: #e33333;
}
.list li{
width: 25%;
background: #ffffff;
float: left;
min-height: 100px;
_height:100px;
box-sizing: border-box;
border: 10px solid #f3f3f3;
text-align: center;
border-radius: 10px;
}
.list li img{
width: 100%;
}
.list li .abtn{
width: 100%;
height: 50px;
display: inline-block;
background: #e33333;
font-weight: bold;
line-height: 50px;
text-decoration: none;
color:#000000;
cursor: pointer;
letter-spacing: 8px;
}
.list li .title{
line-height: 40px;
}
.list li .price{
line-height: 40px;
color: #e33333;
font-size: 18px;
}
3.脚本文件(行为)(文件夹名称:scripts)
封装函数及公用的方法
/*封装的公用函数—开始*/
function $(id) {
return document.getElementById(id);
}
function getTagByClassName(sClassName) {
if (document.getElementsByClassName) {
return document.getElementsByClassName(sClassName);
}
else
{
var allTags=document.getElementsByTagName("*");
var tag=[];
for (var i = 0; i < allTag.length; i++) {
if (allTags[i].className==sClassName) {
tag.push(allTags[i]);
}
}
return tag;
}
}
/*封装的公用函数—完毕*/
//增加或修改cookie
function setCookie(name,value,days,path){
days=days||0;
path=path||'/';
var oDate=new Date();
oDate.setDate(oDate.getDate()+days);
document.cookie = name + '=' + encodeURIComponent(value) + ';expires=' + oDate +';path=' + path;
}
// 获取cookie
function getCookie(name) {
var aCookie = document.cookie.split('; ');
for(var i = 0; i < aCookie.length; i++) {
var temp = aCookie[i].split('=');
if(temp[0] === name) {
return decodeURIComponent(temp[1]);
}
}
}
关于商品列表的脚本
/*网页加载完成后执行的事件*/
window.onload=function() {
var oAbtn=getTagByClassName("abtn");
for (var i = 0; i < oAbtn.length; i++) {
oAbtn[i].onclick=function() {
var goodId=this.getAttribute('data-id');
var goodSrc=this.getAttribute('data-url');
var goodTitle=this.getAttribute('data-title');
var goodPrice=this.getAttribute('data-price');
var goodAmount=this.getAttribute('data-amount');
//创建货物对象并赋值
var oGood={
id:goodId,
src:goodSrc,
title:goodTitle,
price:goodPrice,
amount:goodAmount
};
//读取内存中的cookie信息
var sGoodlist=getCookie("aGoodList")
//获取货物数组,如果货物数组存在则在此基础上进行修改数据,不存在则创建新的数组存储货物对象
var aGoodList=sGoodlist?JSON.parse(sGoodlist):[];
//判断是否存在将要添加到购物车的货物
var whetherExsits=aGoodList.every(function(v) {
if (v.id===oGood.id) {
//存在修改购物车的货物数量
v.amount++;
return false;
}
return true;
});
//不存在则将货物对象追加到数组
if (whetherExsits) {
aGoodList.push(oGood);
}
setCookie("aGoodList",JSON.stringify(aGoodList),7);
}
}
}
关于购物车的脚本
window.onload=function() {
var sGoodlist=getCookie("aGoodList");
var aGoodList=sGoodlist?JSON.parse(sGoodlist):[];
for (var i = 0; i < aGoodList.length; i++) {
var oLi=document.createElement('li');
oLi.innerHTML='<a href="javascript:;" class="abtn" data-id="'+aGoodList[i].id+'">删除该商品</a><img src="'+aGoodList[i].src+'" /><div class="title">'+aGoodList[i].title+'</div><div class="price">¥'+aGoodList[i].price+'/个|数量'+aGoodList[i].amount+'</div>';
$("list").appendChild(oLi);
var oAbtn=getTagByClassName("abtn");
oAbtn[i].index=i;
oAbtn[i].onclick=function() {
//商品数量没删除一次减一件
if (aGoodList[this.index].amount>1) {
aGoodList[this.index].amount--
}
else
{
//剩余一件商品删除该对象
$("list").removeChild(this.parentNode);
aGoodList.splice(this.index,1)//掌握splice的应用
}
//将最新商品保存到cookie
setCookie("aGoodList",JSON.stringify(aGoodList),7);
window.location.reload();
}
}
}
4.图片资源(上方为图片名称)(文件夹名称:images)
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
Get busy living or get busy dying