您好,登錄后才能下訂單哦!
js淘寶購物車功能描述:
1、點(diǎn)擊“+”,單個(gè)商品數(shù)量加1,總數(shù)量加1;單個(gè)商品價(jià)格添加,總價(jià)也添加。
2.點(diǎn)擊“-”,單個(gè)商品數(shù)量減1,總數(shù)量減1;單個(gè)商品價(jià)格減少,總價(jià)也減少。
當(dāng)該商品數(shù)量為0時(shí),點(diǎn)擊依然為0;
3.顯示出總價(jià),總數(shù)量和其中最貴的那個(gè)商品的價(jià)格。
瞄一眼效果圖:
廢話不多說,LU代碼
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</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) { // 如果該商品數(shù)量為0,,那么就得把它的價(jià)格從價(jià)格表中清除 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="+" /> 單價(jià):<em>12.5元</em> 小計(jì):<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價(jià):<em>10.5元</em> 小計(jì):<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價(jià):<em>8.5元</em> 小計(jì):<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價(jià):<em>8元</em> 小計(jì):<span class="highlight">0元</span> </li> <li> <input type="button" value="-" /> <strong>0</strong> <input type="button" value="+" /> 單價(jià):<em>14.5元</em> 小計(jì):<span class="highlight">0元</span> </li> </ul> <p> 商品合計(jì)共:<span class="highlight" id="totalNum">0</span>件, 共花費(fèi)了:<span class="highlight" id="totalPrice">0</span>元<br /> 其中最貴的商品單是:<span class="highlight" id="mostExpensive">0</span>元 </p> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。