直接上代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>購(gòu)物頁(yè)面</title> <style> ul{list-style:none;padding:0;margin:0;} .goods li{display:inline-block;border:1px solid #ddd;padding:10px;margin:10px;} .goods li:hover{} .goods .price{color:#f00;font-weight:bold;} .goods .price::before{ content:"¥"; } </style> <script> window.onload = function(){ var goods = document.getElementsByClassName('goods')[0]; // 用于保存購(gòu)物車商品信息 var carList = []; // 先獲取當(dāng)前cookie var cookies = document.cookie.split('; '); for(var i=0;i<cookies.length;i++){ var arr = cookies[i].split('='); if(arr[0] === 'carlist'){ carList = JSON.parse(arr[1]); } } // 事件委托 goods.onclick = function(e){ e = e || window.event; var target = e.target || e.srcElement; // 添加到購(gòu)物車 if(target.tagName.toLowerCase() === 'button'){ // 獲取當(dāng)前l(fā)i var currentLi = target.parentElement.parentElement; var children = currentLi.children; var currentGUID = currentLi.getAttribute('data-guid'); // 先創(chuàng)建一個(gè)對(duì)象保存當(dāng)前商品信息 var goodsObj = {}; goodsObj.guid = currentGUID; goodsObj.qty = 1; goodsObj.name = children[1].innerHTML; goodsObj.price = children[2].innerHTML; goodsObj.imgUrl = children[0].src; // 如果cookie為空,則直接添加 if(carList.length===0){ // 添加到carList carList.push(goodsObj); }else{ // 先判斷cookie中有無(wú)相同的guid商品 for(var i=0;i<carList.length;i++){ // 如果商品已經(jīng)存在cookie中,則數(shù)量+1 if(carList[i].guid === currentGUID){ carList[i].qty++; break; } } // 如果原cookie中沒有當(dāng)前商品 if(i===carList.length){ // 添加到carList carList.push(goodsObj); } } // 存入cookie // 把對(duì)象/數(shù)組轉(zhuǎn)換誠(chéng)json字符串:JSON.stringify() document.cookie = 'carlist=' + JSON.stringify(carList); } } } </script> </head> <body> <ul class="goods"> <li data-guid="g01"> <img src="images/shirt_1.jpg"> <p>短袖襯衣</p> <p class="price">98.88</p> <div class="add2cart"> <button>添加到購(gòu)物車</button> </div> </li> <li data-guid="g02"> <img src="images/shirt_2.jpg"> <p>短袖襯衣2</p> <p class="price">88.88</p> <div class="add2cart"> <button>添加到購(gòu)物車</button> </div> </li> <li data-guid="g03"> <img src="images/shirt_3.jpg"> <p>短袖襯衣3</p> <p class="price">9.98</p> <div class="add2cart"> <button>添加到購(gòu)物車</button> </div> </li> <li data-guid="g04"> <img src="images/shirt_4.jpg"> <p>短袖襯衣4</p> <p class="price">8.88</p> <div class="add2cart"> <button>添加到購(gòu)物車</button> </div> </li> </ul> <a href="04car.html" rel="external nofollow" >去結(jié)算</a> </body> </html>
//購(gòu)物車頁(yè)面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>購(gòu)物車</title> <style> #carList li{position:relative;padding-bottom:15px;margin-bottom:15px;border-bottom:1px solid #ddd;} #carList img{display:block;width:100px;} #carList li .btn-close{position:absolute;top:0;right:0;padding:0 5px;cursor:default;} #carList li .btn-close:hover{color:#fff;} .subPrice{padding:5px 20px;color:#f00;font-weight:bold;text-align:right;} #carList .price{color:#f00;} .price::before{ content:'¥'; font-size:11px; } #carList .price span{font-size:11px;} </style> <script> window.onload = function(){ /* 讀取cookie中的carlist 把json字符串轉(zhuǎn)換成對(duì)象/數(shù)組:JSON.parse() json字符串格式: 1.必須用雙引號(hào) 2.不能右注釋 */ var oCarList = document.getElementById('carList'); var oSubPrice = oCarList.nextElementSibling; var btnClear = document.getElementById('btnClear'); var carList; var cookies = document.cookie.split('; '); for(var i=0;i<cookies.length;i++){ var arr = cookies[i].split('='); if(arr[0] === 'carlist'){ console.log(JSON.parse(arr[1])); carList = JSON.parse(arr[1]); } } var subPrice = 0; if(carList){ var ul = document.createElement('ul'); for(var i=0;i<carList.length;i++){ var li = document.createElement('li'); // 給每個(gè)li添加data-guid屬性 li.setAttribute('data-guid',carList[i].guid); // 商品名 var title = document.createElement('h4'); title.innerHTML = carList[i].name; // 商品價(jià)格 var price = document.createElement('p'); price.className = 'price'; price.innerHTML = carList[i].price + '×' + carList[i].qty; // 商品圖片 var img = document.createElement('img'); img.src = carList[i].imgUrl; // 添加刪除按鈕 var btnClose = document.createElement('span'); btnClose.innerHTML = '×'; btnClose.className = 'btn-close'; // 計(jì)算總價(jià) subPrice += carList[i].price*carList[i].qty; li.appendChild(title); li.appendChild(price); li.appendChild(img); li.appendChild(btnClose); ul.appendChild(li); } // 寫入頁(yè)面 oCarList.appendChild(ul); // 寫入總價(jià) // toFixed(n)獲取小數(shù)點(diǎn)后n位(自動(dòng)四舍五入,Number類型的方法) oSubPrice.innerHTML = '<span class="price">' + subPrice.toFixed(2) + '</span>'; } // 刪除商品 oCarList.onclick = function(e){ e = e || window.event; var target = e.target || e.srcElement; // 是否點(diǎn)擊了刪除按鈕 if(target.className === 'btn-close'){ var currentLi = target.parentElement; // 獲取當(dāng)前guid var currentGUID = currentLi.getAttribute('data-guid'); // 刪除cookie中對(duì)應(yīng)的商品 // 根據(jù)guid取對(duì)比 for(var i=0;i<carList.length;i++){ // 找出要?jiǎng)h除的商品 if(carList[i].guid === currentGUID){ carList.splice(i,1); break; } } // 更新cookie document.cookie = 'carlist=' + JSON.stringify(carList); // 刪除li節(jié)點(diǎn) currentLi.parentElement.removeChild(currentLi); } } // 清空購(gòu)物車 // 1、刪除DOM節(jié)點(diǎn) // 2、刪除cookie btnClear.onclick = function(){ oCarList.innerHTML = ''; oSubPrice.innerHTML = ''; // 利用設(shè)置有效期位過期事件來(lái)達(dá)到刪除cookie的效果 var now = new Date(); now.setDate(now.getDate()-7); document.cookie = 'carlist=xx;expires=' + now; } } </script> </head> <body> <h1>購(gòu)物車</h1> <div id="carList"> </div> <div class="subPrice"></div> <a href="#" rel="external nofollow" id="btnClear">清空購(gòu)物車</a> </body> </html>
以上這篇原生JS 購(gòu)物車及購(gòu)物頁(yè)面的cookie使用方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com