說明:cookie的操作須有功能變數名稱,簡單點說就是需要用發佈的方式去訪問,查看cookie信息請用開發者模式進入application欄 1.頁面佈局(結構)(根目錄) 商品列表 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> ...
說明: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>
productList.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>
shoppingCart.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;
}
productList.css
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]);
}
}
}
common.js
關於商品列表的腳本
/*網頁載入完成後執行的事件*/
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);
}
}
}
productList.js
關於購物車的腳本
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();
}
}
}
shoppingCart.js
4.圖片資源(上方為圖片名稱)(文件夾名稱:images)
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg