1.html

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ToDoList-最简单的待办事项列表</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/jquery.min.js"></script>
<script src="js/tudolist.js"></script>
</head> <body>
<header>
<section>
<label for="title">ToDoList</label>
<input type="text" id="title" name="title" placeholder="添加ToDo" required="required" autocomplete="off" />
</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>
Copyright &copy; 2021 todolist.cn
</footer>
</body> </html>

2.css

body {
margin: 0;
padding: 0;
font-size: 16px;
background: #CDCDCD;
} header {
height: 50px;
background: #333;
background: rgba(47, 47, 47, 0.98);
} section {
margin: 0 auto;
} label {
float: left;
width: 100px;
line-height: 50px;
color: #DDD;
font-size: 24px;
cursor: pointer;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
} header input {
float: right;
width: 60%;
height: 24px;
margin-top: 12px;
text-indent: 10px;
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
} input:focus {
outline-width: 0
} h2 {
position: relative;
} span {
position: absolute;
top: 2px;
right: 5px;
display: inline-block;
padding: 0 5px;
height: 20px;
border-radius: 20px;
background: #E6E6FA;
line-height: 22px;
text-align: center;
color: #666;
font-size: 14px;
} ol,
ul {
padding: 0;
list-style: none;
} li input {
position: absolute;
top: 2px;
left: 10px;
width: 22px;
height: 22px;
cursor: pointer;
} p {
margin: 0;
} li p input {
top: 3px;
left: 40px;
width: 70%;
height: 20px;
line-height: 14px;
text-indent: 5px;
font-size: 14px;
} li {
height: 32px;
line-height: 32px;
background: #fff;
position: relative;
margin-bottom: 10px;
padding: 0 45px;
border-radius: 3px;
border-left: 5px solid #629A9C;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
} ol li {
cursor: move;
} ul li {
border-left: 5px solid #999;
opacity: 0.5;
} li a {
position: absolute;
top: 2px;
right: 5px;
display: inline-block;
width: 14px;
height: 12px;
border-radius: 14px;
border: 6px double #FFF;
background: #CCC;
line-height: 14px;
text-align: center;
color: #FFF;
font-weight: bold;
font-size: 14px;
cursor: pointer;
} footer {
color: #666;
font-size: 14px;
text-align: center;
} footer a {
color: #666;
text-decoration: none;
color: #999;
} @media screen and (max-device-width: 620px) {
section {
width: 96%;
padding: 0 2%;
}
} @media screen and (min-width: 620px) {
section {
width: 600px;
padding: 0 10px;
}
}

网站布局较为简单,附上html与css作为参考,事件列表通过js动态添加

ToDoList使用本地存储localStorage,可以使数据在浏览器关闭后再次打开不会丢失

$(function () {
//1.按下回车 把数据存储到本地存储里面
//存储的数据格式 var todolist = [{title:"xxx",done:false}]
load()
$("#title").on("keydown", function (e) {
if (e.keyCode === 13) {
if ($(this).val() === "") {
alert("不能为空")
} else {
//先读取本地存储的来源数据
var local = getData();
// console.log(local);
//把local数组进行更新数据把最新的数据追加给local数组
local.push({ title: $(this).val(), done: false });
//把这个数组local 存储给本地存储
saveData(local);
//2.toDoList 本地存储数据渲染加载到页面
load();
$(this).val("")
}
}
});
//3.toDoList删除操作
$("ol,ul").on("click", "a", function () {
//先获取本地存储
var data = getData();
//修改数据
var index = $(this).attr("id")
data.splice(index, 1);
//保存到本地存储
saveData(data)
//重新渲染页面
load()
})
//4.toDoList待完成和已完成选项操作
$("ul,ol").on("click", "input", function () {
// 先获取本地数据
var data = getData();
// 修改数据
var index = $(this).siblings("a").attr("id");
data[index].done = $(this).prop("checked");
// 保存到本地存储
saveData(data)
// 重新渲染页面
load()
})
//读区本地存储的数据
function getData() {
var data = localStorage.getItem("todolist")
if (data != null) {
//本地存储里面数据是字符串格式的,但是我们需要的是对象格式的
return JSON.parse(data)
} else {
return []
}
}
//保存本地存储数据
function saveData(data) {
localStorage.setItem("todolist", JSON.stringify(data))
}
//渲染加载数据
function load() {
var data = getData();
var todoCount = 0;//正在进行个数
var doneCount = 0;//已经完成个数
console.log(data);
//遍历之前先要清空ol里面的元素内容
$("ol,ul").empty();
//遍历
$.each(data, function (i, n) {
// console.log(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)
}
})

模仿ToDoList的更多相关文章

  1. jQuery模仿ToDoList实现简单的待办事项列表

    功能:在文本框中输入待办事项按下回车后,事项会出现在未完成列表中:点击未完成事项前边的复选框后,该事项会出现在已完成列表中,反之亦然:点击删除按钮会删除该事项:双击事项可以修改事项的内容.待办事项的数 ...

  2. TodoList案例

    我们今天模仿ToDoList进行一个简单的增,删,改,查的操作 可参考官网  http://www.todolist.cn/ 下边直接上代码 import React from 'react'; cl ...

  3. JavaScript模仿块级作用域

    avaScript 没有块级作用域的概念.这意味着在块语句中定义的变量,实际上是在包含函数中而非语句中创建的,来看下面的例子: function outputNumbers(count){ for ( ...

  4. 模仿Linux内核kfifo实现的循环缓存

    想实现个循环缓冲区(Circular Buffer),搜了些资料多数是基于循环队列的实现方式.使用一个变量存放缓冲区中的数据长度或者空出来一个空间来判断缓冲区是否满了.偶然间看到分析Linux内核的循 ...

  5. 模仿东京首页banner轮播,京东新闻上下滚动动画实现(动画实现)

    接着上篇 微信小程序-阅读小程序demo写:http://www.cnblogs.com/muyixiaoguang/p/5917986.html 首页banner动画实现 京东新闻上下动画实现 想着 ...

  6. 想着模仿京东微信首页呢,banner滚动搞定了,写到了一半了

    接着上篇 微信小程序-阅读小程序demo写:http://www.cnblogs.com/muyixiaoguang/p/5917986.html 想着模仿京东首页呢,结果也没赶得及写完,轮播图让我搞 ...

  7. js模仿ios select效果

    github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...

  8. JavaScript函数表达式、闭包、模仿块级作用域、私有变量

    函数表达式是一种非常有用的技术,使用函数表达式可以无需对函数命名,从而实现动态编程.匿名函数,是一种强大的方式,一下总结了函数表达式的特点: 1.函数表达式不同于函数声明,函数声明要求有名字,但函数表 ...

  9. Vue.js基础篇实战--一个ToDoList小应用

    距离开始学Vue已经过去一个多月了,总想把学到的东西柔和在一起,做点东西出来,于是有了这个Todolist小应用. 使用vuex 纯粹基础,没有用到web pack,vuex,npm,下次把它改造一下 ...

随机推荐

  1. C# ThreadLocal源码追踪

    ThreadLocal 字段成员: private Func<T>? _valueFactory; 一个获取默认值的委托 不同线程共享此成员. [ThreadStatic] private ...

  2. QT 中的模态和非模态对话框

    void MainWindow::on_pushButton_clicked() { //模态 QDialog dlg(this); dlg.resize(100,100); dlg.exec(); ...

  3. RibbitMQ 实战教程

    # RabbitMQ 实战教程 ## 1.MQ引言 ### 1.1 什么是MQ `MQ`(Message Quene) : 翻译为 `消息队列`,通过典型的 `生产者`和`消费者`模型,生产者不断向消 ...

  4. 实现Comparable接口

    1 import java.util.TreeSet; 2 3 4 /** 5 * PriorityQueue, TreeSet是排序集合,存储的对象必须实现Comparable接口. 6 * 原因是 ...

  5. go defer关键字使用规则

    defer 用于延迟函数的调用,每次defer都会把一个函数压入栈中,函数返回前再把延迟的函数取出并执行 数据结构 type _defer struct { sp uintptr //函数栈指针 pc ...

  6. linux centos7 df命令

    2021-08-04 1. df 命令简介 linux 中 df 命令的功能是用来检查 linux 服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信 ...

  7. Javascirpt 面向对象总结-继承

    JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如下: // 定义一个动物类 function Animal (name) { // 公有属性 this.name = name || ...

  8. Servlet学习笔记(一)之Servlet原理、初始化、生命周期、结构体系

    Servlet是用java语言编写的应用到Web服务器端的扩展技术,与java对象的区别是,Servlet对象主要封装了对HTTP请求的处理,并且它的运行需要Servlet容器的支持(以下会介绍原因, ...

  9. python opencv cv2 imshow threading 多线程

    除了线程同步,还需要注意的是「窗口处理」要放在主线程 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import threadin ...

  10. 20210823 数数,数树,鼠树,ckw的树

    考场 乍一看都不好做 仔细想想发现 T1 的绝对值特别好,轮流选剩余的最大/最小值就行了 T2 又要计数,直接想部分分,发现一个 sb 容斥就有 35ps(但数据锅了,只有 25pts) T3 什么玩 ...