Js实现购物车加减,价格计算等功能需求说明:
1、单击“半闭”按钮时,关闭当前页⾯购物车页⾯
2、单击“移⼊收藏”弹出收藏提⽰
3、单击“删除”弹出确认删除提⽰
4、单击“结算”按钮,弹出结算信息页⾯窗⼝
5、⾃动计算商品总计
6、单击“删除”按钮,使⽤parentNode访问当前节点的⽗亲节点等,使⽤removeChild( )删除当前商品
效果图:
代码:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>完善当当购物车页⾯</title>
<style type="text/css">
body,ul,li,div,p,h1,h2,ol{margin: 0;padding: 0;}
ul,li,ol{list-style: none;}
.content{width: 810px; margin: 0 auto;  font-family: "微软雅⿊";}
.logo{margin: 10px 0;}
.
logo span{
display: inline-block;
float: right;
width: 60px;
height: 30px;
line-height: 30px;
font-size: 14px;
background: #ff0000;
color: #ffffff;
text-align: center;
border-radius: 10px;
margin-top: 5px;
margin-right: 10px;
cursor: pointer;
font-weight: bold;
}
.cartList{
/*background: url("../image/02.jpg") no-repeat;*/
/*height: 414px;*/
overflow: hidden;
}
.cartList ul{
display: flex;
justify-content: space-between;
/*float: right;*/
/*width: 450px;*/
}
.cartList ul:nth-of-type(1){
display: flex;
margin-top: 125px;
}
.cartList ul:nth-of-type(2){
margin: 20px 0;
}
.cartList ul li{
font-family: "微软雅⿊";
font-size: 12px;
color: #666666;
text-align: center;
line-height: 25px;
/*float: left;*/
}
.cartList ul li input[name="price"]{
border: none;
background: transparent;
width: 45px;
text-align: center;
}
.cartList ul li input[name="amount"]{
width: 45px;
text-align: center;
border: 1px solid #999999;
border-left: none;
border-right: none;
height: 21px;
}
.cartList ul li input[name="minus"],.cartList ul li input[name="plus"]{    height: 25px;
border: 1px #999999 solid;
width: 25px;
text-align: center;
}
.cartList ul li:nth-of-type(1){width: 130px;}
.cartList ul li:nth-of-type(2){width: 100px;}
.cartList ul li:nth-of-type(3){width: 130px;}
.cartList ul li p{cursor: pointer;}网页购物车代码
.cartList ol{
float: right;
clear: both;
margin-top: 40px;
}
.cartList ol li{
float: left;
}
.cartList ol li:nth-of-type(1){
color: #ff0000;
width: 80px;
height: 35px;
line-height: 35px;
text-align: center;
}
.cartList ol li span{display: inline-block;
float: right;
width: 80px;
line-height: 35px;
font-size: 14px;
font-family: "微软雅⿊";
background: #ff0000;
color: #ffffff;
text-align: center;
/*margin-top: 5px;*/
/*margin-right: 15px;*/
cursor: pointer;
font-weight: bold;}
</style>
</head>
<!--onload,在加载时计算好原始⾦额-->
<body onload="total()">
<div class="content">
<div class="logo">
<span onclick="javascript:if (confirm('确认要关闭吗'))window.close() ">关闭</span>
</div>
<div class="cartList">
<ul>
<li>商品信息</li>
<li>商品图⽚</li>
<li>单价(元)</li>
<li>数量</li>
<li>⾦额(元)</li>
<li>操作</li>
</ul>
<ul id="first">
<li>《平凡的世界》</li>
<li><img src="./img/1.png" alt="" width="50" height="50"></li>
<li>¥<input type="text" name="price" value="21.90"></li>
<li><input type="button" name="minus" value="-" onclick="minus(0)"><input type="text" name="amount" value="1"><input type="button" name="plus" value            <li id="price0">¥21.90</li>
<li><p onclick="save()">移⼊收藏</p><p onclick="delete1()">删除</p></li>
</ul>
<ul >
<li>《昆⾍记》</li>
<li><img src="./img/2.png" alt="" width="50" height="50"></li>
<li>¥<input type="text" name="price" value="24.00"></li>
<li><input type="button" name="minus" value="-" onclick="minus(1)"><input type="text" name="amount" value="1"><input type="button" name="plus" value            <li id="price1">¥24.00</li>
<li><p onclick="save()">移⼊收藏</p><p onclick="delete1()">删除</p></li>
</ul>
<ol>
<li id="totalPrice"> </li>
<li><span>结算</span></li>
</ol>
</div>
</div>
</body>
</html>
<script>
//减法
function minus(index) {
//获取当前数量的值
var ElementsByName("amount");
//得到第⼀个amount的元素的value属性的值
var count=parseInt(amounts[index].value); //数量加1
alert("不能再减了,快没了!!");
} else {
/
/得到第⼀个amount的元素的value属性的值
var count=parseInt(amounts[index].value)-1; //数量加1
//重新把count的值绑定在数量⽂本框⾥
amounts[index].value=count;
var ElementsByName("price");
var price=parseFloat(prices[index].value);
//乘以Math.pow(10,2)的原因为避免失真
var totalMoney=((price*Math.pow(10,2))*count)/Math.pow(10,2);
}
total();
}
//加法
function plus(index) {
//获取当前数量的值
var ElementsByName("amount");
//得到第⼀个amount的元素的value属性的值
var count=parseInt(amounts[index].value)+1; //数量加1
//重新把count的值绑定在数量⽂本框⾥
amounts[index].value=count;
//当前操作端⼝的价格也要重新计算
//得到当前端⼝的单价
var ElementsByName("price");
var price=parseFloat(prices[index].value);
//乘以Math.pow(10,2)的原因为避免失真
var totalMoney=((price*Math.pow(10,2))*count)/Math.pow(10,2);
//把当前价格显⽰在⽂本中
total();
}
//求总⾦额
function total() {
//得到所有的数量
var ElementsByName("amount");
//得到所有的单价
var ElementsByName("price");
var sumMoney=0;
for (var i=0;i<counts.length;i++){
//乘以Math.pow(10,2)的原因为避免失真
sumMoney+=(parseFloat(prices[i].value)*Math.pow(10,2)*parseInt(counts[i].value)/Math.pow(10,2));        }
//把总⾦额显⽰再指定的元素中
//加⼊收藏
function save() {
if (confirm("确认要收藏吗?")){
alert("收藏成功!");
}
}
//删除
function delete1() {
if (confirm("确认要删除吗?")) {
var ElementById("first");
veChild(del);
alert("删除成功!!");
}
}
</script>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。