JQuery和JS怎样实现淘宝购物车的添加和删除?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js淘宝添加/删除购物车</title>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<style>
* {
margin:0;
padding:0;
font-family:"微软雅黑";
list-style:none;
color:#666;
text-decoration:none;
font-size:14px;
}
body {
background:#f5f5f5;
}
.hid {
overflow:hidden;
}
.left {
float:left;
}
#box {
width:978px;
margin:20px auto;
}
#box ul {
margin-right:-14px;
overflow:hidden;
}
#box li {
width:234px;
float:left;
margin-right:14px;
padding:34px 0 20px;
background:#FFF;
text-align:center;
position:relative;
cursor:pointer;
margin-bottom:14px;
}
.pro_img {
width:150px;
height:150px;
margin:0 auto 18px;
}
.pro_name {
display:block;
text-overflow:ellipsis;
white-space:nowrap;
overflow:hidden;
font-weight:400;
}
.pro_name a {
color:#333;
}
.pro_price {
color:#ff6700;
margin:0 10px 10px;
}
.pro_rank {
color:#b0b0b0;
}
#box li:hover .add_btn {
display:block;/*//设置可见同show*/
/*display:none;//设置隐藏同方法hide()*/
}
.add_btn {
background:#ff6700;
height:66px;
position:absolute;
width:100%;
bottom:0;
left:0;
line-height:66px;
display:none;
color:#FFF;
font-size:20px;
}
.car {
width:978px;
margin:20px auto;
background:#FFF;
}
.car .img {
width:190px;
}
.car .img img {
display:block;
width:80px;
height:80px;
margin:3px auto;
}
.car .name {
width:350px;
}
.car .name span {
line-height:1;
margin-top:8px;
margin-bottom:8px;
font-size:18px;
font-weight:normal;
text-overflow:ellipsis;
white-space:nowrap;
overflow:hidden;
}
.car .price {
width:144px;
text-align:right;
padding-right:84px;
}
.car .price span {
color:#ff6700;
font-size:16px;
}
.car .ctrl {
width:175px;
padding-right:25px;
text-align:center;
}
.car .ctrl a {
font-size:20px;
cursor:pointer;
display:block;
width:26px;
height:26px;
margin:30px auto;
line-height:26px;
}
.car .ctrl a:hover {
color:#FFF;
background:#ff6700;
}
.head_row {
height:70px;
line-height:70px;
}
.head_row,.row {
border-bottom:solid 1px #e0e0e0;
}
.row {
height:86px;
line-height:86px;
padding:15px 0;
}
</style>
</head>
<body>
<div id="box">
    <ul>
    </ul>
</div>
<div id="car" class="car">
    <div class="head_row hid">
        <div class="img left">&nbsp;</div>
        <div class="name left">商品名称</div>
        <div class="price left">单价</div>
        <div class="ctrl left">操作</div>
    </div>
</div><script>
window.onload = function() {
    var aData = [{
            "imgUrl":"images/default.png",
            "proName": "小米移动电源10000mAh",
            "proPrice": "79",
            "proComm": "18"
        },
        {
            "imgUrl": "images/default.png",
            "proName": "小米移动电源10000mAh 高配版",
            "proPrice": "149",
            "proComm": "1.4"
        },
        {
            "imgUrl": "images/default.png",
            "proName": "小米圈铁耳机",
            "proPrice": "99",
            "proComm": "4.2"
        },
        {
            "imgUrl": "images/default.png",
            "proName": "小米活塞耳机 基础版",
            "proPrice": "29",
            "proComm": "3.6"
        },
    ];
    var oBox = document.getElementById("box");
    var oCar = document.getElementById("car");
    var oUl = document.getElementsByTagName("ul")[0];


    for (var i = 0; i < aData.length; i++) {
        var oLi = document.createElement("li");
        var data = aData[i];
        oLi.innerHTML += '<div class="pro_img"><img src="' + data["imgUrl"] + '" width="150" height="150"></div>';
        oLi.innerHTML += '<h3 class="pro_name"><a href="#">' + data["proName"] + '</a></h3>';
        oLi.innerHTML += '<p class="pro_price">' + data["proPrice"] + '元</p>';
        oLi.innerHTML += '<p class="pro_rank">' + data["proComm"] + '万人评价</p>';
        oLi.innerHTML += '<div class="add_btn">加入购物车</div>';
        oUl.appendChild(oLi);
    }
    var aBtn = getClass(oBox, "add_btn");
    for (var i = 0; i < aBtn.length; i++) {
        aBtn[i].index = i;
        aBtn[i].onclick = function() {
            var oDiv = document.createElement("div");
            var data = aData[this.index];
            oDiv.className = "row hid";
            oDiv.innerHTML += '<div class="img left"><img src="' + data["imgUrl"] + '" width="80" height="80"></div>';
            oDiv.innerHTML += '<div class="name left"><span>' + data["proName"] + '</span></div>';
            oDiv.innerHTML += '<div class="price left"><span>' + data["proPrice"] + '元</span></div>';
            oDiv.innerHTML += '<div class="ctrl left"><a href="javascript:;">×</a></div>';
            oCar.appendChild(oDiv);
            var delBtn = oDiv.lastChild.getElementsByTagName("a")[0];
            delBtn.onclick = function() {
                oCar.removeChild(oDiv);
            }
        }
    }


    function getClass(oBox, tagname) {
        var aTag = oBox.getElementsByTagName("*");
        var aBox = [];
        for (var i = 0; i < aTag.length; i++) {
            if (aTag[i].className == tagname) {
                aBox.push(aTag[i]);
            }
        }
        return aBox
    }
}
</script>
</body>
</html>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值