前端练习:ToDoList

模仿ToDoList


ToDoList.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ToDoList—最简单的待办事项列表</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <div class="hander" id="hand">
        <div class="title">
            <form action="javascript:postaction()" id="form">
                <label for="title">ToDoList</label>
                <input type="text" id="title" name="title" placeholder="添加ToDo" required="required" autocomplete="off">
            </form>
        </div>
    </div>

    <div class="section">
        <div class="content">
            <h2>
                正在进行
                <span id="todocount">0</span>
            </h2>
            <div class="demo-box">
                <ol class="todolist" id="todolist">

                </ol>
            </div>
            
            <h2>
                已经完成
                <span id="donecount">0</span>
            </h2>
            <div class="demo-box">
                <ol class="donelist" id="donelist">
  
                </ol>
            </div>
        </div>
    </div>

    <div class="footer">
        Copyright &copy; 2018 todolist.cn <a href="javascript:clear();">clear</a>
    </div>
    


    <script type="text/javascript" src="index.js"></script>
</body>
</html>
index.css
body {
    margin: 0;
    padding: 0;
    font-size: 16px;
    background: #CDCDCD;
}

.hander{
    width: 100%;
    background-color: rgba(47,47,47,0.98);
}

.hander .title{
    width: 600px;
    margin: 0 auto;
    overflow: hidden;
}

.hander .title label{
    float: left;
    width: 100px;
    line-height: 50px;
    color: #DDD;
    font-size: 24px;
    cursor: pointer;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
}

.hander .title input{
    float: right;
    width: 60%;
    height: 24px;
    border-radius: 5px;
    text-indent: 10px;
    margin-top: 12px;
    box-shadow: 0 1px 0 rgba(255,255,255,0.24), 0 1px 6px rgba(0,0,0,0.45) inset;
    border: none;
}
.hander .title input:focus {
    outline-width: 0
}

.section .content{
    width: 600px;
    margin: 0 auto;
}

.section .content h2{
    position: relative;
}

.section .content h2 span{
    position: absolute;
    top: 7px;
    right: 5px;
    padding: 0 5px;
    height: 20px;
    border-radius: 20px;
    background: #E6E6FA;
    line-height: 22px;
    text-align: center;
    color: #666;
    font-size: 14px;
}

ol{
    padding: 0;
}

li{
    list-style: none;
    cursor: move;
}

.section .content li{
    position: relative;
    height: 32px;
    line-height: 32px;
    background-color: #fff;
    margin-bottom: 10px;
    padding: 0 45px;
    border-left: 5px solid #629A9C;
    border-radius: 3px;
}

.section .content li input{
    position: absolute;
    top: 2px;
    left: 10px;
    width: 22px;
    height: 22px;
    cursor: pointer;
}

a{
    text-decoration: none;
}

.section .content li a{
    position: absolute;
    top: 3px;
    right: 5px;
    display: inline-block;
    width: 14px;
    height: 12px;
    border-radius: 14px;
    border: 6px double #FFF;
    background: #CCC;
    line-height: 12px;
    text-align: center;
    color: #FFF;
    font-weight: bold;
    font-size: 14px;
    cursor: pointer;
}

.section .content .donelist li{
    border-left: 5px solid #999;
    opacity: 0.5;
}

/* 编辑框的input */
.section .content li p input {
    top: 3px;
    left: 40px;
    width: 70%;
    height: 20px;
    line-height: 14px;
    text-indent: 5px;
    font-size: 14px;
}

.footer{
    text-align: center;
    color: #666;
    font-size: 14px;
}
.footer a{
    color: #999;
}
index.js
function $(id){
    return document.getElementById(id);
}



function postaction(){
    var title = $('title');
    if(title.value == ''){
        alert("内容不能为空");
    }else{
        var data = loadData();
        var todo = {"title":title.value,"done":false};
        data.push(todo);
        saveData(data);
        load();
    }
}

function loadData(){
    var collection = localStorage.getItem("todo");
    if(collection != null){
        return JSON.parse(collection);
    }
    else return [];
}


function saveData(data){
    localStorage.setItem("todo",JSON.stringify(data));
}

function load(){
    console.log('load');
    
    var collection=localStorage.getItem("todo");
    if(collection != null){
        var data = JSON.parse(collection);
        var todoString = "";
        var doneString = "";
        var todoCount = 0;
        var doneCount = 0;
        for(var i=0;i<data.length;i++){
            if(data[i].done){
                doneString += "<li><input type='checkbox' onchange='javascript:update("+i+",\"done\",false"+")' checked='checked'><p id='p-"+i+"' onclick='javascript:edit("+i+")'>"+data[i].title+"</p><a href='javascript:remove("+i+")'>X</a></li>";
                doneCount += 1;
            }else{
                todoString += "<li><input type='checkbox' onchange='javascript:update("+i+",\"done\",true"+")'><p id='p-"+i+"' onclick='javascript:edit("+i+")'>"+data[i].title+"</p><a href='javascript:remove("+i+")'>X</a></li>";
                todoCount += 1;
            }
        }
        $('todolist').innerHTML = todoString;
        $('todocount').innerText = todoCount;
        $('donelist').innerHTML = doneString;
        $('donecount').innerText = doneCount;
    }else{
        $('todolist').innerHTML = '';
        $('todocount').innerText = 0;
        $('donelist').innerHTML = '';
        $('donecount').innerText = 0;
    }
    
    title.value = '';
}

function update(i,field,value){
    var data = loadData();
    // 删除
    var todo = data.splice(i,1)[0];
    todo[field] = value;
    // 增加
    data.splice(i,0,todo);
    saveData(data);
    load();
}

function remove(i){
    var data = loadData();
    data.splice(i,1)[0];
    saveData(data);
    load();
}

function edit(i)
{
    // load();
    var p = document.getElementById("p-"+i);
    title = p.innerHTML;
    p.innerHTML="<input id='input-"+i+"' value='"+title+"' />";
    var input = document.getElementById("input-"+i);
    // 全选
    input.setSelectionRange(0,input.value.length);
    input.focus();
    console.log(input.value);
    // 失焦
    input.onblur = function(){
        if(input.value == ''){
            p.innerHTML = title;
            alert("内容不能为空");
        }
        else{
            console.log(i,input.value)
            update(i,'title',input.value);
        }
    };
}

function clear(){
    localStorage.clear();
    load();
}

window.onload = load;

转载于:https://www.cnblogs.com/q1ang/p/9879475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值