擊“結算”按鈕,使用節點的層次關系訪問節點,在頁面下方顯示各個商品的價格和所有商品的總價
使用節點屬性和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;
}