js實(shí)現(xiàn)購(gòu)物車加減以及價(jià)格計(jì)算功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)購(gòu)物車加減以及價(jià)格計(jì)算的具體代碼,供大家參考,具體內(nèi)容如下
需求說明:
1、單擊“半閉”按鈕時(shí),關(guān)閉當(dāng)前頁面購(gòu)物車頁面
2、單擊“移入收藏”彈出收藏提示
3、單擊“刪除”彈出確認(rèn)刪除提示
4、單擊“結(jié)算”按鈕,彈出結(jié)算信息頁面窗口
5、自動(dòng)計(jì)算商品總計(jì)
6、單擊“刪除”按鈕,使用parentNode訪問當(dāng)前節(jié)點(diǎn)的父親節(jié)點(diǎn)等,使用removeChild( )刪除當(dāng)前商品
效果圖:
代碼:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>完善當(dāng)當(dāng)購(gòu)物車頁面</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; 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;} </style> </head> <!--onload,在加載時(shí)計(jì)算好原始金額--> <body onload="total()"> <div class="content"> <div class="logo"> <span onclick="javascript:if (confirm('確認(rèn)要關(guān)閉嗎'))window.close() ">關(guān)閉</span> </div> <div class="cartList"> <ul> <li>商品信息</li> <li>商品圖片</li> <li>單價(jià)(元)</li> <li>數(shù)量</li> <li>金額(元)</li> <li>操作</li> </ul> <ul style="display: flex;justify-content: space-between;align-items: center" 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="+" onclick="plus(0)" ></li> <li id="price0">¥21.90</li> <li><p onclick="save()">移入收藏</p><p onclick="delete1()">刪除</p></li> </ul> <ul style="display: flex;justify-content: space-between;align-items: center; margin: 20px 0;"> <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="+" onclick="plus(1)"></li> <li id="price1">¥24.00</li> <li><p onclick="save()">移入收藏</p><p onclick="delete1()">刪除</p></li> </ul> <ol> <li id="totalPrice"> </li> <li><span>結(jié)算</span></li> </ol> </div> </div> </body> </html> <script> //減法 function minus(index) { //獲取當(dāng)前數(shù)量的值 var amounts=document.getElementsByName("amount"); //得到第一個(gè)amount的元素的value屬性的值 var count=parseInt(amounts[index].value); //數(shù)量加1 if (count<=1){ alert("不能再減了,快沒了??!"); } else { //得到第一個(gè)amount的元素的value屬性的值 var count=parseInt(amounts[index].value)-1; //數(shù)量加1 //重新把count的值綁定在數(shù)量文本框里 amounts[index].value=count; var prices=document.getElementsByName("price"); var price=parseFloat(prices[index].value); //乘以Math.pow(10,2)的原因?yàn)楸苊馐д? var totalMoney=((price*Math.pow(10,2))*count)/Math.pow(10,2); document.getElementById("price"+index).innerHTML="¥:"+totalMoney; } total(); } //加法 function plus(index) { //獲取當(dāng)前數(shù)量的值 var amounts=document.getElementsByName("amount"); //得到第一個(gè)amount的元素的value屬性的值 var count=parseInt(amounts[index].value)+1; //數(shù)量加1 //重新把count的值綁定在數(shù)量文本框里 amounts[index].value=count; //當(dāng)前操作端口的價(jià)格也要重新計(jì)算 //得到當(dāng)前端口的單價(jià) var prices=document.getElementsByName("price"); var price=parseFloat(prices[index].value); //乘以Math.pow(10,2)的原因?yàn)楸苊馐д? var totalMoney=((price*Math.pow(10,2))*count)/Math.pow(10,2); //把當(dāng)前價(jià)格顯示在文本中 document.getElementById("price"+index).innerHTML="¥:"+totalMoney; total(); } //求總金額 function total() { //得到所有的數(shù)量 var counts=document.getElementsByName("amount"); //得到所有的單價(jià) var prices=document.getElementsByName("price"); var sumMoney=0; for (var i=0;i<counts.length;i++){ //乘以Math.pow(10,2)的原因?yàn)楸苊馐д? sumMoney+=(parseFloat(prices[i].value)*Math.pow(10,2)*parseInt(counts[i].value)/Math.pow(10,2)); } //把總金額顯示再指定的元素中 document.getElementById("totalPrice").innerHTML="¥:"+sumMoney; } //加入收藏 function save() { if (confirm("確認(rèn)要收藏嗎?")){ alert("收藏成功!"); } } //刪除 function delete1() { if (confirm("確認(rèn)要?jiǎng)h除嗎?")) { var del=document.getElementById("first"); del.parentNode.removeChild(del); alert("刪除成功!!"); } } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ES6中的class是如何實(shí)現(xiàn)的(附Babel編譯的ES5代碼詳解)
這篇文章主要介紹了ES6中的class是如何實(shí)現(xiàn)的?(附Babel編譯的ES5代碼詳解),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05全面解析Bootstrap中tooltip、popover的使用方法
這篇文章主要為大家詳細(xì)解析了Bootstrap中tooltip、popover的使用方法,了解提示框、彈出框的實(shí)現(xiàn)原理,感興趣的朋友可以參考一下2016-06-06javascript實(shí)現(xiàn)控制瀏覽器全屏
這篇文章主要介紹了javascript實(shí)現(xiàn)控制瀏覽器全屏的代碼分享給大家,十分的實(shí)用,有需要的小伙伴可以參考下。2015-03-03Wordpress ThickBox 添加“查看原圖”效果代碼
上一次修改了點(diǎn)擊圖片動(dòng)作 , 這次添加一個(gè)“查看原圖”的鏈接,點(diǎn)擊后將在一個(gè)新瀏覽器窗口(或Tab)打開該圖片的原始鏈接地址。2010-12-12再談javascript圖片預(yù)加載技術(shù)(詳細(xì)演示)
由于javascript無法獲取img文件頭數(shù)據(jù),必須等待其加載完畢后才能獲取真實(shí)的大小,所以lightbox類效果為了讓圖片居中顯示,導(dǎo)致其速度體驗(yàn)要比直接輸出的差很多。2011-03-03javascript入門之?dāng)?shù)組[新手必看]
本文介紹了javascript 數(shù)組的定義和數(shù)組元素的操作,ECMAScript中的數(shù)組方法...希望對(duì)大家有所幫助2016-11-11uniapp自定義應(yīng)用退出執(zhí)行內(nèi)容實(shí)例代碼
近幾日使用uni-app開發(fā)移動(dòng)應(yīng)用APP遇到了個(gè)不常見的需求,下面這篇文章主要給大家介紹了關(guān)于uniapp自定義應(yīng)用退出執(zhí)行內(nèi)容的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12