js淘寶購物車功能描述:
1、點擊“+”,單個商品數量加1,總數量加1;單個商品價格添加,總價也添加。
2.點擊“-”,單個商品數量減1,總數量減1;單個商品價格減少,總價也減少。
當該商品數量為0時,點擊依然為0;
3.顯示出總價,總數量和其中最貴的那個商品的價格。
瞄一眼效果圖:

廢話不多說,LU代碼
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><style>*{padding:0;margin:0;}#list,p{list-style:none;width:600px;margin:0 auto;}#list li {width:600px;height:50px;line-height:50px;margin-top:20px;font-size:20px;}#list li input{width:60px;height:40px;line-height:40px;}.highlight{color:red;font-size:30px;font-weight:bold;}</style><script>window.onload = function () { var oUl = $('list'); var totalNumber = $('totalNum'); var totalPrice = $('totalPrice'); var mostExpensive = $('mostExpensive'); var aLi = oUl.getElementsByTagName('li'); var maxPrice = [0]; for ( var i = 0; i < aLi.length; i++ ) { price(aLi[i]); } function price(oLi) { var aBtn = oLi.getElementsByTagName('input'); var oStrong = oLi.getElementsByTagName('strong')[0]; var oEm = oLi.getElementsByTagName('em')[0]; var oSpan = oLi.getElementsByTagName('span')[0]; aBtn[0].onclick = function () { var num = Number(oStrong.innerHTML); var price = parseFloat(oEm.innerHTML); var numbers = Number(totalNumber.innerHTML); var prices = parseFloat(totalPrice.innerHTML); num--; if (num === 0) { // 如果該商品數量為0,,那么就得把它的價格從價格表中清除 var index = maxPrice.indexOf(price); if (index > -1) maxPrice.splice(index, 1); } else if (num < 0) { num = 0; return; } numbers--; oStrong.innerHTML = num; oSpan.innerHTML = num * price + '元'; totalNumber.innerHTML = numbers; totalPrice.innerHTML = prices - (num + 1) * price; mostExpensive.innerHTML = maxPrice[0]; } aBtn[1].onclick = function () { var num = Number(oStrong.innerHTML); var price = parseFloat(oEm.innerHTML); var numbers = Number(totalNumber.innerHTML); var prices = parseFloat(totalPrice.innerHTML); num++; numbers++; if (maxPrice.indexOf(price) < 0) { maxPrice.push(price) maxPrice.sort(function (a,b) {return b - a}); } oStrong.innerHTML = num; oSpan.innerHTML = num * price + '元'; totalNumber.innerHTML = numbers; totalPrice.innerHTML = prices + num * price; mostExpensive.innerHTML = maxPrice[0]; } } function $(id) {return document.getElementById(id);}}</script></head><body><ul id="list"> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價:<em>12.5元</em> 小計:<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價:<em>10.5元</em> 小計:<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價:<em>8.5元</em> 小計:<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價:<em>8元</em> 小計:<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價:<em>14.5元</em> 小計:<span class="highlight">0元</span> </li></ul><p>商品合計共:<span class="highlight" id="totalNum">0</span>件,共花費了:<span class="highlight" id="totalPrice">0</span>元<br />其中最貴的商品單是:<span class="highlight" id="mostExpensive">0</span>元</p></body></html>以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答