赞
踩
<div class="main"> <div class="header"> <div class="logo">todoList</div> <input type="text" placeholder="请输入..." id="input"/> </div> <div class="doing todo"> <h3> <span class="title">正在进行</span> <span class="num"></span> </h3> <div class="list"> <!--<div class="todoItem">--> <!--<input type="checkbox">--> <!--<div class="content">内容</div>--> <!--<div class="del">删除</div>--> <!--</div>--> </div> </div> <div class="done todo"> <h3> <span class="title">已经完成</span> <span class="num" ></span> </h3> <div class="list"> <!--<div class="todoItem">--> <!--<input type="checkbox">--> <!--<div class="content">内容</div>--> <!--<div class="del">删除</div>--> <!--</div>--> </div> </div> </div>
*{ margin: 0; padding: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body{ background-color: #cccccc; font-size: 16px; } .main{ width: 3.75rem; } .header{ width: 3.75rem; height: 0.5rem; background-color: #87ceeb; display: flex; justify-content: space-between; align-items: center; } .header> .logo{ width: 1.2rem; height: 0.5rem; font-weight: bold; text-align: center; line-height: 0.5rem; font-size: 0.25rem; } .header> input{ height: 0.3rem; width: 2.5rem; margin: 0 0.2rem; border-radius: 0.05rem; border: none; outline: none; padding: 0 0.1rem; } .todo h3{ height: 0.6rem; line-height: 0.6rem; display: flex; justify-content: space-between; align-items: center; padding: 0 0.15rem; } .todo .list{ padding: 0 0.15rem; } .todo .todoItem{ display: flex; height: 0.38rem; line-height: 0.38rem; align-items: center; background: #888888; border-radius: 0.05rem; overflow: hidden; margin-bottom: 0.1rem; } .todo .todoItem:before{ width: 0.04rem; height: 0.38rem; background: #00B7FF; content: ""; } .todo .todoItem>input{ width: 0.2rem; height: 0.2rem; margin: 0 0.2rem; } .todo .todoItem .content{ width: 2.6rem; } .todo .todoItem .del{ background: #ff6700; width: 0.4rem; height: 0.2rem; font-size: 0.12rem; line-height: 0.2rem; text-align: center; border-radius: 0.05rem; color: #cccccc; } .done.todo .todoItem{ opacity: 0.3; } .pc .main{ width: 600px; }
var doingListDiv = document.querySelector(".doing .list"); //正在进行的内容部分
var doneListDiv = document.querySelector(".done .list"); //已经完成的内容
var mainDiv = document.querySelector(".main"); //为了统一控制删除模块
var doingnumDiv = document.querySelector(".doing .num"); //正在进行的内容个数
var donenumDiv = document.querySelector(".done .num"); //已经完成的内容个数
if (localStorage.todoList == undefined){ //如果本地内存没有数据
var todoList = []; //初始化todoList
}else {
var todoList = JSON.parse(localStorage.todoList); //字符串转换为对象
}
var inputDom = document.querySelector("#input"); //获取输入对象
//监听输入
inputDom.onkeydown = function (event) {
//当按下回车,获取输入框内容,生成未完成列表
if (event.key == 'Enter'){
var value = inputDom.value; //将获取的对象存为value
var objList = { //创建对象存数据
content:value,
isDone: false //默认:正在进行
};
todoList.push(objList); //将获取的输入对象传入todoList
render(todoList); //渲染
}
}
function render(todoList){ localStorage.todoList = JSON.stringify(todoList); //将对象转换为JSON 字符串 //每次渲染要将其置为“”,避免重复传值 doingListDiv.innerHTML = ''; doneListDiv.innerHTML = ''; todoList.forEach(function (item,index) { //对于todoList的每一项进行渲染 var todoItem = document.createElement('div'); //为每一个传入内容创建Item todoItem.className = "todoItem"; if (item.isDone){ //判断属于正在进行还是完成 todoItem.innerHTML = //插入Item,其中变量每一项的内容 `<input type="checkbox" checked="checked" data-index="${index}"> <div class="content">`+ item.content+`</div> <div class="del">删除</div>`; doneListDiv.appendChild(todoItem); } else { todoItem.innerHTML = `<input type="checkbox" data-index="${index}"> <div class="content">`+ item.content+`</div> <div class="del">删除</div>`; doingListDiv.appendChild(todoItem); } }) //得到正在进行的内容数量 var doingnum = document.querySelectorAll(".doing .todoItem").length; console.log(doingnum); doingnumDiv.innerHTML = doingnum; //得到已经完成的内容数量 var donenum = document.querySelectorAll(".done .todoItem").length; console.log(donenum); donenumDiv.innerHTML = donenum; } render(todoList); //从本地存储获取数据的渲染
doingListDiv.onchange = function(e){ //事件触发 var index = parseInt(e.target.dataset.index); //获取当前选中的index todoList[index].isDone = true; //通过改变isDone的值来渲染 render(todoList); } doneListDiv.onchange = function(e){ //事件触发 var index = parseInt(e.target.dataset.index); //获取当前选中的index todoList[index].isDone = false; //通过改变isDone的值来渲染 render(todoList); } mainDiv.onclick = function(e){ //点击删除事件 if (e.target.className == 'del'){ var index = parseInt(e.target.dataset.index); todoList.splice(index,1); //数组删除 render(todoList); } } ``’
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。