ajax怎么实现购物车加减,js实现仿购物车加减效果

效果图:

d8c2fbe9d55c313dd9c3687d23be7464.png

代码如下:

*{

margin: 0;

padding: 0;

}

body{

overflow-y: auto;

}

ul{

margin-top: 20px;

border-top: 1px solid #666;

}

h1{

width: 500px;

margin: 0 auto;

color: deeppink;

height: 100px;

line-height: 100px;

}

li{

list-style: none;

padding: 15px 0px 15px 60px;

border-bottom:1px solid #ccc;

font-size: 0px;

line-height: 30px;

/*height: 60px;*/

}

input[type=button],li strong,li em,li span,li div{

height: 30px;

font-size: 20px;

line-height: 30px;

text-align: center;

}

input[type=button],li strong

{

width: 60px;

}

li span,li em{

width: 80px;

background: pink;

}

li div{

width: 100px;

}

li strong,li span,li em,li div{

display: inline-block;

}

window.οnlοad=function(){

var oUl=document.getElementById('list');

var aLi=oUl.getElementsByTagName('li');

for(var i=0;i

fn1(aLi[i]);

}

function fn1(aLi){

var aBtn=aLi.getElementsByTagName('input');

var aStrong=aLi.getElementsByTagName('strong')[0];

var aEm=aLi.getElementsByTagName('em')[0];

var aSpan=aLi.getElementsByTagName('span')[0];

var num=Number(aStrong.innerHTML); //aStrong.innerHTML='0'

var price=parseFloat(aEm.innerHTML);//aEm.innerHTML='12.9元'

aBtn[0].οnclick=function(){

if(num>0)

{

num--;

aStrong.innerHTML=num;

aSpan.innerHTML=(num*price).toFixed(1)+'元'

}

}

aBtn[1].οnclick=function(){

num++;

aStrong.innerHTML=num;

aSpan.innerHTML=(num*price).toFixed(1)+'元'

}

}

}

点点看,仿购物车+ -效果

  • 0

    单价:
    12.9元
    共计:
    0
  • 0

    单价:
    45.7元
    共计:
    0
  • 0

    单价:
    67.5元
    共计:
    0
  • 0

    单价:
    14.7元
    共计:
    0
  • 0

    单价:
    45.6元
    共计:
    0
  • 0

    单价:
    32.4元
    共计:
    0

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package shoppingcart.biz; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * 购物车 */ public class ShoppingCart { private Map cartMap = null; //保存Product的Map /** * 购物车构造函数 */ public ShoppingCart(){ cartMap = new HashMap(); } /** * 取得存放产品的cartMap */ public Map getCartMap(){ return this.cartMap; } /** * 向购物车中添加产品 */ public boolean addProduct(String productId){ if(existProduct(productId)){ // 产品已存在则增加数量 Product product = cartMap.get(productId); product.setProductNum(product.getProductNum() + 1); return true; } else{ // 否则新加入该产品 Product product = new Product(productId); if(product.getProductId()==null){ return false; // 数据库中无该产品 }else{ cartMap.put(productId, product); return false; } } } /** * 检查购物车里是否已存在该产品 */ public boolean existProduct(String productId){ Iterator hmkey = cartMap.keySet().iterator(); while(hmkey.hasNext()){ String pid = hmkey.next(); if(pid.equals(productId)){ return true; } } return false; } /** * 从购物车中移除指定产品 */ public void delProduct(String productId){ cartMap.remove(productId); } /** * 获取购物车产品数量 */ public int getProductNum(){ int productNum = 0; Iterator hmEntry = cartMap.values().iterator(); while(hmEntry.hasNext()){ productNum += hmEntry.next().getProductNum(); } return productNum; } /** * 统计购物车商品总价 */ public double getTotalprice(){ double totalPrice = 0.0; Iterator hmkey = cartMap.keySet().iterator(); Product product = null; while(hmkey.hasNext()){ product = cartMap.get(hmkey.next()); totalPrice += product.getProductPirce() * product.getProductNum(); } return totalPrice; } /** * 清空购物车 */ public void clearCart(){ cartMap.clear(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值