To Do List (待办事项)

To Do List (待办事项)

上篇博客中我们讲到了浏览器的本地存储,因为没有列出具体的实例让大家操作,所以肯定还有一部分人是没有彻底明白的,那么今天这篇博客,就通过一个案例来让大家好好了解一下什么是本地存储以及本地存储的作用!

闲话少说,直接上干货!

To Do List (待办事项)案例

名称:

  • To Do List (待办事项)

需求:

  1. 前端页面的开发使用响应式布局,使得用户在移动端上也可以使用
  2. 输入事项以后,用户按回车添加到未完成目录中
  3. 未完成和已完成可以通过复选框相互转化
  4. 浏览器关闭以后数据不会丢失

具体操作:

index.html
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>待办事项</title>
    <link rel="stylesheet" href="./cs.css">
    <link rel="stylesheet" href="./css.css">
    <script src="./jquery-3.5.1.min.js"></script>
    <script src="./js.js"></script>

</head>

<body>
    <header>
        <section>
            <form id="form">
                <label for="title">待办事项</label>
                <input type="text" id="title" placeholder="添加待办事项!" />
            </form>
        </section>
    </header>
    <section>
        <h2>正在进行 <span id="todocount"></span></h2>
        <ol id="todolist" class="demo-box">
        </ol>
        <h2>已经完成<span id="donecount"></span></h2>
        <ul id="donelist">
        </ul>
    </section>
    <footer>
        待办事项 &copy; 2020 别.cn BIE
    </footer>
</body>

</html>
cs.css
html {
    font-size: 16px;
  }
  @media screen and (max-width: 750px) {
    html {
      font-size: 16px;
    }
  }
  @media screen and (max-width: 540px) {
    html {
      font-size: 10px;
    }
  }
  @media screen and (max-width: 360px) {
    html {
      font-size: 5px;
    }
  }
  
css.css
* {
    padding: 0px;
    margin: 0px;
    box-sizing: border-box;
    
}
body {
    background: #CDCDCD;
    
}
header {
    height: 3.125rem;
    background: #333;
    background: rgba(47,47,47,0.98);
}
label {
    float: left;
    width: 6.25rem;
    line-height: 3.125rem;
    color: #DDD;
    font-size: 1.5rem;
    margin-left: 30%;
}
header input {
    float: right;
    width: 20%;
    height: 1.5rem;
    margin-top: .75rem;
    text-indent: .625rem;
    border-radius: 5px;
    box-shadow: 0 1px 0 rgba(255,255,255,0.24), 0 1px 6px rgba(0,0,0,0.45) inset;
    border: none;
    margin-right: 30%;
}
h2 {
    margin-left: 30%;
    display: block;
    font-size: 1.5rem;
    margin-top: 0.83rem;
    margin-bottom: 0.83rem;
    font-weight: bold;
}
footer {
    margin-top: 0.83rem;
    font-size: 1rem;
    width: 30%;
    margin-left: 40%;
}
h2 {
    position: relative;
}
span {
    position: absolute;
    top: .125rem;
    right: 43%;
    display: inline-block;
    padding: 0 .3125rem;
    height: 1.25rem;
    border-radius: 1.25rem;
    background: #E6E6FA;
    line-height: 1.375rem;
    text-align: center;
    color: #666;
    font-size: .875rem;
}
ol,ul {
    list-style: none;
}
li {
    margin-left: 30%;
    width: 40%;
    height: 2rem;
    line-height: 2rem;
    background-color: #fff;
    position: relative;
    margin-bottom: .625rem;
    padding: 0px 2.8125rem;
    border-radius: .1875rem;
    box-shadow: 0 .0625rem .125rem rgba(0,0,0,0.7);
}
li input {
    position: absolute;
    top: .125rem;
    left: .625rem;
    width: 1.375rem;
    height: 1.375rem;
}
li a {
    position: absolute;
    top: .125rem;
    right: .3125rem;
    display: inline-block;
    width: 1.8rem;
    height: 1.8rem;
    border-radius: 9rem;
    border: .375rem solid #fff;
    background-color: #ccc;
    line-height: .875rem;
    text-align: center;
    color: lightgray;
    font-weight: 25px;
    font-size: .875rem;
}
js.js
$(function () {
    // 刷新一次页面并且读取数据
    load();
    // 添加键盘事件
    $("#title").on("keydown", function (event) {
        if (event.keyCode === 13) {
            // 先读取本地存储原来的数据
            var local = getDate();
            // 把local数组进行更新数据 把最新的数据追加给local数组
            local.push({ title: $(this).val() });
            // 把这个数组local 存储给本地存储
            saveDate(local);
            // 2. toDoList 本地存储数据渲染加载到页面
            load();
            $(this).val("");


        }
    });
    // toDoList 删除操作
    $("ol, ul").on("click", "a", function () {
        // 先获取本地存储
        var data = getDate();
        // 修改数据
        var index = $(this).attr("id");
        data.splice(index, 1);
        // 保存到本地存储
        saveDate(data);
        // 重新渲染页面
        load();
    });
    // toDoList 正在进行和已完成选项操作
    $("ol, ul").on("click", "input", function () {
        // 先获取本地存储的数据
        var data = getDate();
        // 修改数据
        var index = $(this).siblings("a").attr("id");
        data[index].done = $(this).prop("checked");
        // 保存到本地存储
        saveDate(data);
        // 重新渲染页面
        load();
    });
    // 读取本地存储的数据
    function getDate() {
        //读取已储存的json数据
        var data = localStorage.getItem("todolist");
        if (data !== null) {
            // 本地存储里面的数据是字符串格式的 但是我们需要的是对象格式的
            return JSON.parse(data);
        } else {
            return [];
        }
    }
    // 保存本地存储数据
    function saveDate(data) {
        //已json形式存储
        localStorage.setItem("todolist", JSON.stringify(data));
    }
    // 渲染加载数据
    function load() {
        // 读取本地存储的数据
        var data = getDate();
        // 遍历之前先要清空ol里面的元素内容
        $("ol, ul").empty();
        var todoCount = 0; // 正在进行的个数
        var doneCount = 0; // 已经完成的个数
        // 遍历这个数据
        $.each(data, function (i, n) {
            if (n.done) {
                $("ul").prepend("<li><input type='checkbox' checked='checked' > <p>" + n.title + "</p> <a href='javascript:;' id=" + i + " ></a></li>");
                doneCount++;
            } else {
                $("ol").prepend("<li><input type='checkbox' > <p>" + n.title + "</p> <a href='javascript:;' id=" + i + " ></a></li>");
                todoCount++;
            }
        });
        $("#todocount").text(todoCount);
        $("#donecount").text(doneCount);
    }

})

以上就是我们To Do List (待办事项)的全部内容,相信大家通过这一个案例.对浏览器的本地存储有了更加深入的了解,那么今天的内容就到这里了,大家有什么不懂的地方,可以私信问我,我会尽力为大家进行讲解!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值