87,916
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>shopCart</title>
<style>
*{ margin:0; padding:0; border:0; list-style:none; font-style:normal; font-weight:100; }
body{ padding:20px; font:normal normal 100 14px/1.6 arial; color:#888; text-transform:capitalize; background:#fff; }
a,
a:hover{ text-decoration:none; }
/*=shop cart*/
.shopcart{ margin:0 auto; width:500px; padding:10px; border:1px solid #ccc; }
.list{ text-transform:none; }
.list li{ padding:0 6px; border-bottom:1px solid #eee; overflow:hidden; }
.list li:hover{ background:#fafafa; }
.list_i{ display:block; height:30px; line-height:30px; overflow:hidden; }
.list_i:hover{ background:#fafafa; }
.list_itit,
.list_inum,
.list_iprice{ display:block; width:380px; height:30px; white-space:nowrap; text-overflow:ellipsis; overflow:hidden; float:left; }
.list_itit{ color:#00c; }
.list_inum{ width:38px; height:14px; line-height:14px; padding:4px; border:1px solid #ddd; margin:3px 0; }
.list_iprice{ width:60px; text-align:right; color:#f60; }
/****************/
.list_sub{ padding:4px 0; border-top:1px solid #eee; }
.list_isub{ line-height:22px; overflow:hidden; }
.list_istit{ width:400px; float:left; }
.list_istit input{ vertical-align:middle; }
.list_istit span{ padding:0 0 0 4px; }
.list_isprice{ width:88px; float:left; text-align:right; color:#f60; }
/****************/
.total_msg{ padding:10px 10px 0; text-align:right; }
.total_msg strong{ padding:0 2px; color:#222; }
.total_msg .totalprice{ color:#f60; font-size:18px; }
</style>
</head>
<body>
<div class="shopcart">
<ul class="list">
<li>
<p class="list_i"><span class="list_itit">iPhone</span><input type="number" value="0" min="1" class="list_inum"><span class="list_iprice">560</span></p>
<div class="list_sub">
<p class="list_isub"><label class="list_istit"><input type="checkbox" name="list_fitting"><span>Fitting 01</span></label><span class="list_isprice">5</span></p>
<p class="list_isub"><label class="list_istit"><input type="checkbox" name="list_fitting"><span>Fitting 02</span></label><span class="list_isprice">2</span></p>
<p class="list_isub"><label class="list_istit"><input type="checkbox" name="list_fitting"><span>Fitting 03</span></label><span class="list_isprice">8</span></p>
<p class="list_isub"><label class="list_istit"><input type="radio" name="list_fitting"><span>Fitting 04</span></label><span class="list_isprice">9</span></p>
<p class="list_isub"><label class="list_istit"><input type="radio" name="list_fitting"><span>Fitting 05</span></label><span class="list_isprice">12</span></p>
</div>
</li>
<li>
<p class="list_i"><span class="list_itit">iMac</span><input type="number" value="0" min="1" class="list_inum"><span class="list_iprice">3800</span></p>
</li>
<li>
<p class="list_i"><span class="list_itit">Macbook Pro</span><input type="number" value="0" min="1" class="list_inum"><span class="list_iprice">2900</span></p>
</li>
</ul>
<div class="total_msg">
<p>count:<strong class="totalnum">0</strong></p>
<p>total price:<strong class="totalprice">0</strong>$</p>
</div>
</div>
<script>
/*=shop cart*/
/*=data:json*/
</script>
</body>
</html>
function Cart(key) {
var sc = localStorage[key];
if (sc) try { this.products = JSON.parse(sc); } catch (ex) { }
if (!this.prodocuts) this.products = [];
}
生成购物车的实例的时候已经从localstorage初始化过了,如果有其他的才需要后续的步骤,就是“//通过你上面的DOM对象初始化cart对象”, 这个是什么意思,DOM对象初始化Cart对象”,有可能你进入的页面有localstorage未存储的其他物品
function Product(name, price, num, id, belongid) {
this.name = name; //产品名称
this.price = price;//价格
this.num = num; //数量
this.id = id; //产品唯一id
this.belongid = belongid; //配件属于哪个产品的id,如果不是配件就设置为0或-1什么的
}
function Cart(key) {
var sc = localStorage[key];
if (sc) try { this.products = JSON.parse(sc); } catch (ex) { }
if (!this.prodocuts) this.products = [];
}
Cart.prototype.findProduct = function (id) {//通过id查找产品所在数组下标
for (var i = 0; i < this.products.length; i++) if (this.products[i].id == pro.id) return i;
return -1;
}
Cart.prototype.add = function (pro) {
var index = this.findProduct(pro.id);
if (index != -1) this.products[index] = pro.num;//存在则只需要更新数量就行了
else this.products.push(pro);
}
Cart.prototype.remove = function (id) { var index = this.findProduct(pro.id); if (index != -1) this.products.splice(index, 1); }
Cart.prototype.toString = function () { return JSON.stringify(this.products); } //重写toString方法,将产品序列化为字符串
Cart.prototype.save = function (key) { localStorage[key] = this.toString(); }
//使用和实例名称
var shopCart
window.onload = function () {
shopCart = new Cart('shopingcart');
//通过你上面的DOM对象初始化cart对象,或者通过ajax获取产品对象,然后add下
console.log(shopCart)
}
window.onbeforeunload = function () { shopCart.save('shopingcart'); }//页面卸载时保存