设为首页 加入收藏

TOP

购物车案例(一)
2019-09-23 11:17:24 】 浏览:93
Tags:购物车 案例

学习前端以来,一直很好奇像京东,淘宝这种大型网站的购物车是怎么做到统一的,就去搜索了一些资料吧!大致的看了一下,自己实战了下,俗话说的好,读万卷书不如行万里路!购物车只是一个很小的案例,但也可以去动手做一做,感受下过程!积少成多!

这案例主要是通过本地储存来实现的,我用了两种方法,一种是cookie,另一种是localStorage。cookie会跟随http发送给服务器,并且有时间限制,在没有设置时间时,默认过期时间是关闭浏览器就过期;localStorage是本地的,永久的,储存的数据也更大!这是cookie和localStorage的区别!

!!!敲重点,我所写的都是基于原生,没有使用jQuery!

就先用cookie来实现下效果吧!

  1. 目前没有后端,那就先自己模拟下后端传输数据;准备一个json文件,里面存放的就是数据!类似下图这样!
  2. 引入之前封装好的ajax和cookie的js文件,也可引入jQuery,但jQuery没有提供cookie的方法,只有插件,网上下一个即可!
      下面是封装ajax的代码
     1 function ajax(options){
     2     var {type,url,success,data,error,timeout}=options;
     3     data=data||{};
     4     type=type||"get";
     5     timeout=timeout||2000;
     6     var str="";
     7     for(var i in data){
     8         str+=`${i}=${data[i]}&`;
     9     }
    10     if(type=="get"){
    11         var d=new Date();
    12         url=url+"?"+str+"_yjyt="+d.getTime();
    13     }
    14     var xhr=new XMLHttpRequest();
    15     xhr.open(type,url);
    16     // console.log(url);
    17     xhr.onreadystatechange=function(){
    18         if(xhr.readyState==4&&xhr.status==200){
    19             success&&success(xhr.responseText);
    20             error=null;
    21         }else if(xhr.readyState==4&&xhr.status!=200){
    22             error&&error(xhr.status);
    23             success=null;
    24             error=null;
    25         }
    26     }
    27     setTimeout(()=>{
    28         error&&error(timeout);;
    29         success=null;
    30     },timeout);
    31     if(type=="post"){
    32         xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    33         xhr.send(str);
    34     }else{
    35         xhr.send();
    36     }
    37 }

     接下来的是cookie的代码

     1 function setCookie(key, val, options) {
     2     // console.log(options.expires);
     3     options = options || {};
     4     var path = "";
     5     if (options.path) {
     6         path = ";path=" + options.path;
     7     }
     8     var expires = "";
     9     if (options.expires) {
    10         var d = new Date();
    11         d.setDate(d.getDate() + options.expires);
    12         expires = ";expires=" + d;
    13         console.log(expires);
    14     }
    15     document.cookie = key + "=" + val + path + expires;
    16 }
    17 function getCookie(key) {
    18     // var arr=document.cookie;
    19     // console.log(arr);
    20     var arr = document.cookie.split(";");
    21     console.log(arr);
    22     for (var i = 0; i < arr.length; i++) {
    23         if (arr[i].split("=")[0] === key) {
    24             return arr[i].split("=")[1];
    25         }
    26     }
    27     return "";
    28 }
    29 function removeCookie(key, options) {
    30     options = options || {};
    31     options.expires = -1;
    32     // console.log(options);
    33     // console.log(key);
    34     setCookie(key, 23, options);
    35 }
  3. 接下来就是布局,简单点来,感受下就好,不好看可以自己后期通过css改,这都是写小问题哈,我只说关键的地方!布局简陋,可以自行发挥哦!
    显示商品列表的布局
    1     <h2>这是商品列表<a href="car-local.html">去结算</a></h2>
    2     <div class="cont">
    3         <p>不好意思,卖完了!</p>
    4     </div>

     购物车的布局

     1     <h2>这是结算页<a href="shop.html">继续购物</a></h2>
     2     <table border="1" rules="all" width="900" align="center">
     3         <thead>
     4             <tr>
     5                 <th>图片</th>
     6                 <th>名字</th>
     7                 <th>价格</th>
     8                 <th>数量</th>
     9                 <th>操作</th>
    10             </tr>
    11         </thead>
    12         <tbod
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇jQuery-动画 下一篇attr()、prop()、css() 的区别

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目