击“结算”按钮,使用节点的层次关系访问节点,在页面下方显示各个商品的价格和所有商品的总价
使用节点属性和element属性消除浏览器兼容性
首先写主体html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>商品</title> <link type="text/css" rel="stylesheet"href="css/Dang.css"/> </head> <body> <div class="content"> <div class="logo"> <img src="img/dd_logo.jpg" /> <span onclick="close_plan()">关闭</span> </div> <div class="cartList" id="cartList"> <ul> <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="+" onclick="plus(0);"></li> <li id="price0">¥19.9=00</li> <li><p onclick="collection();">移入收藏</p ><p onclick="del();">删除</p ></li> </ul> <ul> <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="+" onclick="plus(1);"></li> <li id="price1">¥24.00</li> <li><p onclick="collection();">移入收藏</p ><p onclick="del();">删除</p ></li> </ul> <ol> <li id="totalPrice"> </li> <li><span onclick="accounts();">结 算</span></li> </ol> </div> <div></div> </div> <script type="text/javascript" src="js/shopping.js"></script> <script type="text/javascript"> //这里的js可以放到js包下使用 function accounts(){ var num1=document.getElementById("cartList").firstElementChild.firstElementChild.nextElementSibling.nextElementSibling.innerHTML||document.getElementById("cartList").firstChild.firstChild.nextSibling.nextSibling.innerHTML; var num2=document.getElementById("cartList").firstElementChild.nextElementSibling.lastElementChild.previousElementSibling.innerHTML||document.getElementById("cartList").firstChild.nextSibling.lastChild.previousSibling.innerHTML var total=document.getElementById("cartList").lastElementChild.firstElementChild.innerHTML||document.getElementById("cartList").lastChild.firstChild.innerHTML; var str="您本次购买的商品信息如下:<br/>白岩松:白说:"+num1+"<br/>岛上书店:"+num2+"<br/>商品共计:"+total; document.getElementById("cartList").nextElementSibling.innerHTML=str; } </script> </body> </html>
然后是js包
function close_plan(){ window.close(); } function collection(){ var flag =confirm("移动收藏后,将不再购物车显示,是否继续操作?"); if(flag==true){ alert("移动收藏成功!") } } function del(){ var flag =comfirm("您确定要删除商品吗?"); if(flag==true){ alert("删除成功!"); } } function minus(num){ //爆红区域因为没有调用到,在主体部分调用 var prices=document.getElementsByName("price")[num].value; var count=parseInt(document.getElementsByName("amount")[num].value)-1; if(count<1){ alert("不能在减了,我已经被掏空了"); }else{ document.getElementsByName("amount")[num].value=count; var totals=parseFloat(prices*count); document.getElementById("price"+num).innerHTML="¥"+totals; } } function plus(num){ var prices=document.getElementsByName("price")[num].value; var count=parseInt(document.getElementsByName("amount")[num].value)+1; document.getElementsByName("amount")[num].value=count; var totals=parseFloat(prices*count); document.getElementById("price"+num).innerHTML="¥"+totals; total(); } function total(){ var prices=document.getElementsByName("price"); var count=document.getElementsByName("amount"); var sum=0; for(var i =0;i<prices.length;i++){ sum+=prices[i].value*count[i].value; } document.getElementById("totalPrice").innerHTML="¥"+sum.toFixed(2); } total();
让页面更美观 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(../img/shoppingBg.jpg) no-repeat;
height: 414px;
overflow: hidden;
}
.cartList ul{
float: right;
width: 450px;
}
.cartList ul:nth-of-type(1){
margin-top: 125px;
}
.cartList ul:nth-of-type(2){
margin-top:70px;
}
.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: 60px;
}
.cartList ol li{
float: left;
}
.cartList ol li:nth-of-type(1){
color: #ff0000;
width: 120px;
}
.cartList ol li span{display: inline-block;
float: right;
width: 80px;
height: 35px;
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;}
.content div:last-of-type{
border: 1px #FF0000 solid;padding: 5px;
}
原文:https://www.cnblogs.com/304979850w/p/13158687.html