模仿ToDoList
ToDoList.html
ToDoList—最简单的待办事项列表
正在进行
0
已经完成
0
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[i].title+"
X";
doneCount += 1;
}else{
todoString += ""+data[i].title+"
X ";
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="";
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;