zoukankan      html  css  js  c++  java
  • 前端练习: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{
         100%;
        background-color: rgba(47,47,47,0.98);
    }
    
    .hander .title{
         600px;
        margin: 0 auto;
        overflow: hidden;
    }
    
    .hander .title label{
        float: left;
         100px;
        line-height: 50px;
        color: #DDD;
        font-size: 24px;
        cursor: pointer;
        font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    }
    
    .hander .title input{
        float: right;
         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- 0
    }
    
    .section .content{
         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;
         22px;
        height: 22px;
        cursor: pointer;
    }
    
    a{
        text-decoration: none;
    }
    
    .section .content li a{
        position: absolute;
        top: 3px;
        right: 5px;
        display: inline-block;
         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;
         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;
    
  • 相关阅读:
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    [PHP类] 分享一个强大的HTTP访问类(可做采集)
    php程序检测页面是否被百度收录
    php文章相似度计算 不用similar_text()函数
    php实现天干地支计算器示例 php算命程序
    Nginx 1.5.2 + PHP 5.5.1 + MySQL 5.6.10 在 CentOS 下的编译安装
    call和apply实现的继承
    Html语义化标签
  • 原文地址:https://www.cnblogs.com/q1ang/p/9879475.html
Copyright © 2011-2022 走看看