今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的增删改查,也许写的并不是那么完善,但最重要的是一个入门,也希望js达人给些建议,好让我更上一个台阶。 
HOHO~~~开始咯: 
Js: 
复制代码 代码如下:
 
//为了省事,就没写自己的js ajax了 用了jquery的,当然你也可以添加到jquery的扩展方法内,哈哈,我太懒了,所以就写这里了。 
var _$ = { AJAX: function (urlparm, d, beforecall, successcall) { 
$.ajax({ 
url: "ashx/ajax_shoppingCart.ashx?" + urlparm, 
data:d, 
dataType:"Json", 
type: "POST", 
before: beforecall, 
success:successcall 
}); 
} 
}; 
(function () { 
var Jusoc = {}; 
Jusoc = { 
_inital: function () { window.Jusoc = Jusoc; }, 
Base: {}, 
DAO: {}, 
BLL: {}, 
UI: {} 
} 
Jusoc.Base = { 
Validate: { 
} 
} 
//AJAX() 
Jusoc.DAO = { 
Shopping: { 
Get: function (beforecall, successcall) { 
_$.AJAX("action=get", null, beforecall, successcall); 
}, 
Remove: function (pid, beforecall, successcall) { 
_$.AJAX("action=remove", { "pid": pid }, beforecall, successcall); 
}, 
Add: function (pid, pcount, beforecall, successcall) { 
_$.AJAX("action=add", { "pid": pid, "pcount": pcount }, beforecall, successcall); 
}, 
Set: function (pid, pcount, beforecall, successcall) { 
_$.AJAX("action=set", { "pid": pid, "pcount": pcount }, beforecall, successcall); 
} 
} 
} 
Jusoc.BLL = { 
Shopping: (function () { 
var Data = null; 
var isLock = false; 
var _successcall = null; 
var _beforecall = null; 
function Unlock() { 
isLock = false; 
} 
function Lock() { 
isLock = true; 
if(Data&&Data !=null) 
{ 
Data = null; 
} 
} 
function CallBackFunction(xhr) { 
Unlock(); 
// if (xhr[0] == "ERROR") { 
// alert(xhr[1]); 
// return; 
// } 
// else if (xhr[0] == "SUCCESS") { 
// Jusoc.BLL.Shopping.SetData(xhr[1]); 
// } 
Jusoc.BLL.Shopping.SetData(xhr); 
if (_successcall != null && _successcall) { 
_successcall.call(window, xhr); 
} 
_successcall = null; 
} 
function PrepareRequst(beforecall, successcall) { 
if (isLock) { 
return false; 
} 
Lock(); 
if (beforecall != null && beforecall) { 
_beforecall = beforecall; 
} 
if (successcall != null && successcall) { 
_successcall = successcall; 
} 
} 
return { 
Get: function (beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Get(_beforecall, CallBackFunction); 
}, 
Remove: function (pid, beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Remove(pid, _beforecall, CallBackFunction); 
}, 
Set: function (pid, pcount, beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Set(pid, pcount, beforecall, CallBackFunction); 
}, 
Add: function (pid, pcount, beforecall, successcall) { 
if(PrepareRequst(beforecall, successcall)==false)return false; 
Jusoc.DAO.Shopping.Add(pid, pcount, _beforecall, CallBackFunction); 
}, 
GetData: function () { 
//alert(Data); 
return Data; 
}, 
SetData: function (data) { Data = data; }, 
RemoveData: function () { 
if (Data != null && Data) 
Data= null; 
} 
} 
})(), 
XHR: { 
} 
} 
Jusoc.UI = { 
ShoppingCart: (function () { 
function Constract() { 
Jusoc.BLL.Shopping.Get(null,SetShoppingCart); 
} 
function SetShoppingCart(data) { 
//这里来填充购物车中的数据 
var data = Jusoc.BLL.Shopping.GetData(); 
//这里 先构建 整个的购物车 
var html = "<table class=https://www.jb51.net/article/\"shoppingcart-list\" id=https://www.jb51.net/article/\"sm\">"+ 
"<tr>"+ 
"<th>"+ 
"书啊"+ 
"</th>"+ 
"<th>"+ 
"书名"+ 
"</th>"+ 
"<th>"+ 
" 单价"+ 
"</th>"+ 
"<th>"+ 
" 数量"+ 
"</th>"+ 
"<th>"+ 
" 操作"+ 
"</th>"+ 
"</tr>"; 
for(var i =0;i<data.length;i++) 
{ 
html += "<tr>"+ 
"<td>"+ 
"<img src=https://www.jb51.net/article/\"ss\" height=https://www.jb51.net/article/\"36px\" width=https://www.jb51.net/article/\"28px\"/>"+ 
"</td>"+ 
"<td>"+ 
data[i].Name+ 
"</td>"+ 
"<td>"+ 
"¥"+data[i].Money+ 
"</td>"+ 
"<td>"+ 
"<div class=https://www.jb51.net/article/\"item-change\">"+ 
"<input type=https://www.jb51.net/article/\"text\" value='"+data[i].Count+"' />"+ 
"<span title=https://www.jb51.net/article/\"数量加一\" class=https://www.jb51.net/article/\"add\" onclick=https://www.jb51.net/article/\"Jusoc.UI.ShoppingCart.Plus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span> <span "+ 
"title=https://www.jb51.net/article/\"数量减一\" class=https://www.jb51.net/article/\"cut\" onclick=https://www.jb51.net/article/\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span>"+ 
"</div>"+ 
"</td>"+ 
"<td>"+ 
"<span class=https://www.jb51.net/article/\"RemoveLink\" onclick=https://www.jb51.net/article/\"Jusoc.UI.ShoppingCart.Remove(1,this.parentNode.parentNode)\">Remove From Cark</span>"+ 
"</td>"+ 
"</tr>"; 
} 
html+="</table>"; 
document.body.innerHTML+=html; 
} 
function AddToPanel(data) { 
//这里是对 添加一个商品到购物车 来修改前台样式 
var obj = document.getElementById("sm"); 
var html = "<td>"+ 
"<img src=https://www.jb51.net/article/\"soo\" height=https://www.jb51.net/article/\"36px\" width=https://www.jb51.net/article/\"28px\"/>"+ 
"</td>"+ 
"<td>"+ 
data.Name+ 
"</td>"+ 
"<td>"+ 
"¥"+data.Money+ 
"</td>"+ 
"<td >"+ 
"<div class=https://www.jb51.net/article/\"item-change\">"+ 
"<input type=https://www.jb51.net/article/\"text\" value='"+data.Count+"' />"+ 
"<span title=https://www.jb51.net/article/\"数量加一\" class=https://www.jb51.net/article/\"add\" onclick=https://www.jb51.net/article/\"Jusoc.UI.ShoppingCart.Plus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span> <span "+ 
"title=https://www.jb51.net/article/\"数量减一\" class=https://www.jb51.net/article/\"cut\" onclick=https://www.jb51.net/article/\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span>"+ 
"</div>"+ 
"</td>"+ 
"<td>"+ 
"<span class=https://www.jb51.net/article/\"RemoveLink\" onclick=https://www.jb51.net/article/\"Jusoc.UI.ShoppingCart.Remove(1,this.parentNode.parentNode)\">Remove From Cark</span>"+ 
"</td>"; 
var row = obj.insertRow(1); 
row.innerHTML = html; 
return; 
obj.childNodes[0].innerHTML += html; 
} 
function UpdatePanel(obj, count) { 
//这里是从购物车中 增加 或者 减少 修改操作 
obj.value = count; 
} 
function RemoveFromPanel(child) 
{ 
var obj = document.getElementById("sm"); 
obj.childNodes[0].removeChild(child); 
} 
return { 
PageLoad: function () { 
Constract(); 
}, 
Add: function (pid, pcount) { 
Jusoc.BLL.Shopping.Add(pid,pcount, null, AddToPanel); 
}, 
Plus: function (pid, pcount, obj) { 
pcount = parseInt(pcount) + 1; 
Jusoc.BLL.Shopping.Set(pid, pcount, function () { alert("before") }, function (data) { UpdatePanel(obj, pcount) }); 
}, 
Minus:function(pid,pcount,obj){ 
pcount = parseInt(pcount) - 1; 
Jusoc.BLL.Shopping.Set(pid,pcount,null,function(data){ UpdatePanel(obj,pcount)}); 
}, 
Remove:function(pid,obj){ 
Jusoc.BLL.Shopping.Remove(pid,null,function(data){ RemoveFromPanel(obj);}); 
} 
} 
})() 
} 
Jusoc._inital(); 
})() 
Tips:这里的显示页面仅仅是demo,如需要,可以自己定制。
HTML:
复制代码 代码如下:
