<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>购物页面</title> <style> img{ width:300px; height:300px; } ul{ list-style:none; padding:0; margin:0; } button{ width:200px; height:50px; text-align: center; color:white; background:black; font-size: 30px; cursor: pointer; } a{ display: block; margin:0 auto; width:200px; height:80px; text-align: center; line-height :80px; color:white; background:red; font-size: 60px; font-weight: 900; text-decoration: none; cursor: pointer; } .goods li{ display:inline-block; border:1px solid #ddd; padding:10px; margin:10px; text-align: center; } .goods li:hover{ background-color:#efefef; } .goods .price{ color:#f00; font-weight:900; } .goods .price::before{ content:"¥"; } </style> <script src = "../cookie.js"></script> <script> window.onload = function(){ var goods = document.getElementsByClassName(‘goods‘)[0]; // 用于保存购物车商品信息 var carList = []; // 先获取当前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]); } } // getcookie(carList); // 事件委托 goods.onclick = function(e){ e = e || window.event; var target = e.target || e.srcElement; // 添加到购物车 if(target.tagName === ‘BUTTON‘){ // 获取当前li var currentLi = target.parentElement.parentElement; var children = currentLi.children; var currentGUID = currentLi.getAttribute(‘data-guid‘); // 先创建一个对象保存当前商品信息 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中有无相同的guid商品 for(var i=0;i<carList.length;i++){ // 如果商品已经存在cookie中,则数量+1 if(carList[i].guid === currentGUID){ carList[i].qty++; break; } } // 如果原cookie中没有当前商品 if(i===carList.length){ // 添加到carList carList.push(goodsObj); } } // 存入cookie // 把对象/数组转换诚json字符串:JSON.stringify() document.cookie = ‘carlist=‘ + JSON.stringify(carList); } } } </script> </head> <body> <ul class="goods"> <li data-guid="g01"> <img src="images/2.png"> <p>短袖衬衣</p> <p class="price">98.88</p> <div class="add2cart"> <button>添加到购物车</button> </div> </li> <li data-guid="g02"> <img src="images/3.png"> <p>短袖衬衣2</p> <p class="price">88.88</p> <div class="add2cart"> <button>添加到购物车</button> </div> </li> <li data-guid="g03"> <img src="images/4.png"> <p>短袖衬衣3</p> <p class="price">9.98</p> <div class="add2cart"> <button>添加到购物车</button> </div> </li> <li data-guid="g04"> <img src="images/5.png"> <p>短袖衬衣4</p> <p class="price">8.88</p> <div class="add2cart"> <button>添加到购物车</button> </div> </li> </ul> <a href="res.html">去结算</a> </body> </html> <!--购物车页面 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>购物车</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{ background-color:#f00; color:#fff; } .subPrice{ padding:5px 20px; color:#f00; font-weight:900; text-align:right; } #carList .price{ color:#f00; } .price::before{ content:‘¥‘; font-size:11px; } #carList .price span{ font-size:11px; } </style> <script src ="../cookie.js"></script> <script> window.onload = function(){ /* 读取cookie中的carlist 把json字符串转换成对象/数组:JSON.parse() json字符串格式: 1.必须用双引号 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]); } } // getcookie(carList); var subPrice = 0; if(carList){ var ul = document.createElement(‘ul‘); for(var i=0;i<carList.length;i++){ var li = document.createElement(‘li‘); // 给每个li添加data-guid属性 li.setAttribute(‘data-guid‘,carList[i].guid); // 商品名 var title = document.createElement(‘h4‘); title.innerHTML = carList[i].name; // 商品价格 var price = document.createElement(‘p‘); price.className = ‘price‘; price.innerHTML = carList[i].price +" X "+ carList[i].qty; // 商品图片 var img = document.createElement(‘img‘); img.src = carList[i].imgUrl; // 添加删除按钮 var btnClose = document.createElement(‘span‘); btnClose.innerHTML = ‘ב; btnClose.className = ‘btn-close‘; // 计算总价 subPrice += carList[i].price*carList[i].qty; li.appendChild(title); li.appendChild(price); li.appendChild(img); li.appendChild(btnClose); ul.appendChild(li); } // 写入页面 oCarList.appendChild(ul); // 写入总价 // toFixed(n)获取小数点后n位(自动四舍五入,Number类型的方法) oSubPrice.innerHTML = ‘<span >‘ + subPrice.toFixed(2) + ‘</span>‘; } // 删除商品 oCarList.onclick = function(e){ e = e || window.event; var target = e.target || e.srcElement; // 是否点击了删除按钮 if(target.className === ‘btn-close‘){ var currentLi = target.parentElement; // 获取当前guid var currentGUID = currentLi.getAttribute(‘data-guid‘); // 删除cookie中对应的商品 // 根据guid取对比 for(var i=0;i<carList.length;i++){ // 找出要删除的商品 if(carList[i].guid === currentGUID){ carList.splice(i,1); break; } } // 更新cookie document.cookie = ‘carlist=‘ + JSON.stringify(carList); // 删除li节点 currentLi.parentElement.removeChild(currentLi); } } // 清空购物车 // 1、删除DOM节点 // 2、删除cookie btnClear.onclick = function(){ oCarList.innerHTML = ‘‘; oSubPrice.innerHTML = ‘‘; // 利用设置有效期位过期事件来达到删除cookie的效果 var now = new Date(); now.setDate(now.getDate()-7); document.cookie = ‘carlist=xx;expires=‘ + now; } } </script> </head> <body> <h1>购物车</h1> <div id="carList"> </div> <div class="subPrice"></div> <a href="#" id="btnClear">清空购物车</a> </body> </html>
推荐文章
支付宝手机网页支付类解决方法TP5小程序+公众号一键安装:http://github.crmeb.net/u/crmeb支付宝手机网页端支付实用类 下载官方手机网站支付SDK&Demo php版本 https://docs.open.alipay.com/203/105910/ 1 <?php 2 namespa
推荐文章
hexo高阶教程:next主题优化之加入网易云音乐、网易云跟帖、炫酷动态背景、自定义样式,打造属于你自己的定制化博客
前言 本篇文章是在已经搭建好gitpage+hexo的博客的前提下(不懂怎么搭建的可以参考我的另一篇博文:了解githubPages+hexo搭建博客的原理 或者利用Gitpage+hexo开发自己的博客,这两篇博文都比较详细的教大家最基础的怎么将博客搭建起来。本篇博文是使用next主题的进击版本,主要是有以下内容 域名绑定,将git
推荐文章
[开源]ApolloClr一个新的IL解释器,并且能运行在Web上
[开源]ApolloClr一个新的IL解释器,并且能运行在Web上 导言 隔壁有L#,又有ILRuntime,理论上不应该写这个东西。 最近补.net IL 写Aop,想想顺手写个IL解释器。还能解决下协程实现问题,也就开了这个项目,希望努力能写完吧。 GitHub地址 Web预览版地址,点击运行 期望 1.至少比以上2个实现都稍
推荐文章
处理git commit 冲突 commit your changes or stash them before you can merge.
在github上fork了一个分支,过了一段时间,想合入主分支的内容。 执行 git fetch upstream git merge upstream/master 遇到了下面的问题: [html] view plain copy
推荐文章
最近做的一个项目涉及到评分和展示分数的模块,UI设计师也给了几个切好的图片,实现五角星评分方式很多,本质爱折腾的精神和对性能追求以及便于维护的考虑,搜集和尝试了很多方式,最终采用了纯css驱动的实现方式完成评分和展示分数的功能,没有js,也就意味着没判断逻辑,代码出错的几率更少,也更便于维护,在此,把这个功能的实现的过程记录和分享一下,一起学习
推荐文章
https://www.cnblogs.com/digital-wei/p/5925502.html 一、前言 在数字芯片设计中常常涉及不同的工作时钟域,在异步时钟域间控制交互、数据交互又涉及异步电路设计。良好、健壮的异步电路设计可提高系统的稳定性、可靠性、健壮性。本博文介绍异步电路中的脉冲同步设计方法。 二、应用 在设计开发过
推荐文章
HTMLTestRunner_PY3.py支持 python3.x github python3版本
github源码下载地址:https://github.com/huilansame/HTMLTestRunner_PY3/archive/master.zip 解压后进入文件找到文件:HTMLTestRunner_PY3.py,将其放入C:Python37Libsite-packages 内 代码演示调用以及报告效果,自己使用随意写代
推荐文章
原生js实现简单的Ripple按钮 效果如图 准备食材(html部分) <ul > <li> <a href='#'> <span>首页</span> <span &
推荐文章
本文主要介绍如何在 Sublime Text 3 搭建 Git 环境,关于这两者,就不多加介绍了,懂者自懂。我会从头开始搭建并连接 GitHub 远程仓库进行简单的代码提交更新等操作。 特别提醒:本文的操作系统为 Windows,但绝大多数步骤与 Mac 类似。 Git 安装 这边提个醒,有些同学喜欢使用 GitHub 客户端,而该客户
推荐文章
webhook革命: 使用fish-hook自动部署多个应用
fish-hook 一站式高效管理你多个github webhook. Github地址: https://github.com/dcalsky/fi... 背景 等你辛苦建立好了静态博客,却依然要忍受每次本地更新后,还要ssh到远程重新git pull一遍的痛苦。 当你终于用webhook handler写了一堆代码来解决
推荐文章
由OpenDigg 出品的前端开源项目周报第七期来啦。我们的前端开源周报集合了OpenDigg一周来新收录的优质的前端开源项目,方便前端开发人员便捷的找到自己需要的项目工具等。 lottie-react-native 实时渲染After Effects动画 react-navigation 学习一次随时随地导航 nachos
推荐文章
因为工作需要将Fortigate的配置文件转换为HTML表格,写了几个Shell-script程序。上传到GitHub上了。 有兴趣的朋友可以去看看。 URL 如下: https://github.com/ChoBon/Parse-fortigate-configuration-files 说明: 本程序可以将Fort
推荐文章
前一阵子一直在想怎么捣鼓出自己的博客来,直到发现了hexo hexo是什么 Hexo是一个开源的静态博客生成器,用node.js开发,作者是台湾大学生tommy351 同样是大学生,我和他的差距怎么这么大 TT 她是一个快速、简洁且高效的博客框架。hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生
推荐文章
由OpenDigg 出品的前端开源项目周报第三期来啦。我们的前端开源周报集合了OpenDigg一周来新收录的优质的前端开发方面的开源项目,方便前端开发人员便捷的找到自己需要的项目工具等。 Cost of modules 查看哪个依赖拖慢了速度 csspin 模块化可定制单一HTML元素代码 react-tetris 用R
推荐文章
登陆认证、web api接口调用、支付接口调用等场合经常涉及到:md5、sh、 rsa等算法
PHP-RSA简介 github 地址 https://github.com/lmxdawn/PH... 实际项目中的登陆认证、web api接口调用、支付接口调用等场合经常涉及到:md5、sh、 rsa等算法。各大银行接口中经常使用MD5算法对调用接口参数进行签名防篡改。 如果你和我有同样的问题 : web api调用认证中,客户端和
推荐文章
版权声明:此文首发于我的简书账号人生还有多少个二十年 ,转载请注明出处。 在慕课网看到有人提了这个问题,所以在这里说说自己的一些拙见。 我不算过来人,本没资格谈论这个,不过我还是忍不住想说说自己的看法。HTML、CSS、JS要熟练掌握,要跟进时代,还必须学HTML5、CSS3、ES6。框架的话,css的要掌握一个bootstr