js 购物车的实现
购物车原理:创建一个构造函数,把涉及到的项目写成方法,然后把这些方法放到构造函数的原型对象上,通过按钮绑定,调用原型对象上的方法,实现对涉及项目的改变
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物车</title>
<script src="./js/gouwu.js"></script>
<link rel="stylesheet" href="./css/gouwu.css">
</head>
<body>
<table>
<thead>
<tr>
<th class="tdone">序号</th>
<th class="tdtwo">商品名称</th>
<th class="tdthree">数量</th>
<th class="tdfour">单价</th>
<th class="tdfive">小计</th>
<th class="tdsix">操作</th>
</tr>
</thead>
<tbody>
<tr class="trclass">
<td class="tdone xuhao">1</td>
<td class="tdtwo ">烤煎饼</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">2</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr class="trclass">
<td class="tdone xuhao">2</td>
<td class="tdtwo">珍珠奶茶</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">3.5</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr class="trclass">
<td class="tdone xuhao">3</td>
<td class="tdtwo">羊肉串</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">1.5</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr>
<td class="tdone xuhao">4</td>
<td class="tdtwo">牛肉</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">10</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr class="trclass">
<td class="tdone xuhao">5</td>
<td class="tdtwo">烧刀子</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">1.8</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr class="trclass">
<td class="tdone xuhao">6</td>
<td class="tdtwo">水煮鱼</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">15</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr class="trclass">
<td class="tdone xuhao">7</td>
<td class="tdtwo">花生米</td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>单价:</span><span class="unit">3</span></td>
<td class="tdfive"><span>小计:</span><span class="subtal">0</span></td>
<td class="tdsix"><button class="del">删除</button></td>
</tr>
<tr><td colspan="6"; class="talast"><span>商品一共 <span class="goods_num">0</span> 件; 共计花费 <span class="pricetal">0</span> 元; 其中最贵的商品单价是 <span class="pricest">0</span> 元</span></td></tr>
</tbody>
</table>
</body>
</html>
css代码:
table{
width:50%;
position:relative;
margin:30px auto;
border-collapse: collapse;
border:1px solid gray;
}
th{
background: cornflowerblue;
height:2.5em;
}
.tdone{
width:10%;
}
.tdtwo{
width:20%;
}
.tdthree{
width:20%;
}
.tdfour{
width:20%;
}
.tdfive{
width:20%;
}
.tdsix{
width:10%;
}
td{
height:2em;
text-align: center;
border:1px solid #ccc;
}
.num{
display:inline-block;
width:3em;
}
input{
height:2em;
}
.talast{
text-align: left;
}
js代码:
window.onload = function(){
function cart(){
this.abtn = document.querySelectorAll('input');
this.ogood_num = document.querySelector('.goods_num');
this.opricetal = document.querySelector('.pricetal');
this.opricest = document.querySelector('.pricest');
this.totalnum = 0;
};
//小计: 商品数量 * 商品单价
cart.prototype.getsubtotal = function(goodsnum,unitprice){
return parseInt(goodsnum) * parseFloat(unitprice) ;
};
// 计算商品的总计花费
cart.prototype.gettotal = function(){
var asubtotal = document.querySelectorAll('.subtal');
var res = 0;
for(var i=0,len=asubtotal.length;i<len;i++){
res += parseFloat(asubtotal[i].innerHTML);
};
return res;
};
// 寻找购物车中已经选中的产品的最大单价
cart.prototype.compareMaxunit = function(){
var anum = document.querySelectorAll('.num');
var aunit = document.querySelectorAll('.unit');
var temp = 0;
for(var i=0,len=anum.length;i<len;i++){
if(anum[i].innerHTML!=0){
if(temp<parseFloat(aunit[i].innerHTML)){
temp = parseFloat(aunit[i].innerHTML);
}
}
};
return temp;
};
// 点击“+”号按钮触发的购物车商品数量,花费,最大价格的变动
cart.prototype.plus = function(obtn){
var onum = obtn.parentNode.querySelector('.num');
var n = parseInt(onum.innerHTML);
onum.innerHTML = ++n ;
this.totalnum++;
var oUnit = obtn.parentNode.parentNode.parentNode.querySelector('.unit');
var osubtotal = obtn.parentNode.parentNode.parentNode.querySelector('.subtal');
osubtotal.innerHTML = this.getsubtotal(onum.innerHTML,oUnit.innerHTML);
this.ogood_num.innerHTML = this.totalnum;
this.opricetal.innerHTML = this.gettotal();
this.opricest.innerHTML = this.compareMaxunit();
};
// 点击“-”号按钮触发的购物车商品数量,花费,最大价格的变动
cart.prototype.minus = function(obtn){
var onum = obtn.parentNode.querySelector('.num');
if(parseInt(onum.innerHTML)>0){
var n = parseInt(onum.innerHTML);
onum.innerHTML = --n ;
this.totalnum--;
var oUnit = obtn.parentNode.parentNode.parentNode.querySelector('.unit');
var osubtotal = obtn.parentNode.parentNode.parentNode.querySelector('.subtal');
osubtotal.innerHTML = this.getsubtotal(onum.innerHTML,oUnit.innerHTML);
this.ogood_num.innerHTML = this.totalnum;
this.opricetal.innerHTML = this.gettotal();
this.opricest.innerHTML = this.compareMaxunit();
}
};
// 获取已买商品的总计数量
cart.prototype.getNumbertal = function(){
var anum = document.querySelectorAll('.num');
var res_num = 0;
for(var i =0;i<anum.length;i++){
res_num += parseInt( anum[i].innerHTML ) ;
}
return res_num ;
}
// 删除按钮触发的购物车商品数量,花费,最大价格的变动
cart.prototype.del = function(obtn){
var odel = obtn.parentNode.parentNode;
var oparent = odel.parentNode;
oparent.removeChild(odel);
this.ogood_num.innerHTML = this.getNumbertal();
this.opricetal.innerHTML = this.gettotal();
this.opricest.innerHTML = this.compareMaxunit();
this.xuhaosort();
}
// 购物车序号重新排序
cart.prototype.xuhaosort = function(){
var axuhao = document.querySelectorAll('.xuhao');
for(var i=0,len=axuhao.length;i<len;i++){
axuhao[i].innerHTML = i+1;
}
}
// 绑定“+”,“-”,删除按钮的点击事件
cart.prototype.bind = function(){
var that = this ;
for(var i=0;i<this.abtn.length;i++){
if(i%2 !=0){
this.abtn[i].onclick = function(){
that.plus(this);
}
}else{
this.abtn[i].onclick = function(){
that.minus(this);
}
}
};
var delbtn = document.querySelectorAll('.del');
for(var i=0;i<delbtn.length;i++){
delbtn[i].onclick = function(){
that.del(this);
}
}
};
var oCart = new cart();
oCart.bind();
}
运行结果:
点击“-”号按钮结果:
点击“+”号按钮结果:
点击“删除”按钮结果:
js 购物车的实现的更多相关文章
- 简单实用angular.js购物车功能
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- js购物车计算价格
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...
- vue.js购物车
vue.js https://cn.vuejs.org/v2/guide/ 简单购物车 <html> <head> <script src="https://c ...
- 原生JS 购物车及购物页面的cookie使用
////////////////////////////////////购物页面 <!DOCTYPE html><html lang="en"><he ...
- JS购物车编辑
实现了:第一件商品的加减实现了:全选/全不选(使用prop而不是attr)实现了:删除(遮罩层) 未实现:第二件商品的删除未实现:小计及应付款额的初始化(写死的) 计算小数乘法时,要先乘100 < ...
- js 购物车中,多件商品数量加减效果修改,实现总价随数量加减改变
<!DOCTYPE html> <html> <head> <meta charset=UTF-8 /> <title>无标题文档</ ...
- js 购物车的数量加减,对应的总价也随机变化
html相关的源码: <div class="goods_num clearfix"> <div class="num_name fl"> ...
- Vue node.js商城-购物车模块
一.渲染购物车列表页面 新建src/views/Cart.vue获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){ return { car ...
- shoppingCart.js
ylbtech-JavaScript-util: shoppingCart.js 购物车脚本 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1,m. ...
随机推荐
- Eureka与ZooKeeper 的比较(转)
https://www.cnblogs.com/zgghb/p/6515062.html Eureka的优势 1.在Eureka平台中,如果某台服务器宕机,Eureka不会有类似于ZooKeeper的 ...
- android切换前后台状态监听
public class BaseApplication extends Application { private static BaseApplication instance; /** * 当前 ...
- [SQL]卸载数据库清理注册表方法regedit
.打开注册表. 开始——运行——regedit——确定 .然后找到下面的文件夹,删除掉: HKEY_CURRENT_USER\ Software\ Microsoft\ Microsoft SQL S ...
- 卷积神经网络之GoogleNet:inceptionV3模型学习
Rethinking the Inception Architecture for Computer Vision 论文地址:https://arxiv.org/abs/1512.00567 Abst ...
- sbt 以及 IDEA sbt 插件安装配置教程(转)
1. 在Windows中安装sbt 下载 官网: http://www.scala-sbt.org/ github: https://github.com/sbt/sbt/releases/downl ...
- cas server
Tomcat: V8.5.x Java: 1.8 x64 MySQL: 5.5.x OS: Win10 x64 I. war 0. clone git clone https://github.com ...
- jQuery对象的操作
参考一篇不错的文章:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object 1. ...
- jq 上传下载进度条
里面只演示了下载的,挂载的是我的七牛服务器上的内容,上传事件和下载是一模一样的,为了大家不乱上传东西到我的服务器,而且我的服务器容量也不大,这里只展示了下载.代码: <!DOCTYPE html ...
- Linux下zip命令
解压命令(解压.覆盖解压) unzip zip unzip -o xxx.zip 压缩命令(支持多个文件或目录) zip -r xxx.zip xxx yyy.txt /a/b/c
- Maven项目jar包的依懒范围