JQuery实现todolist案例
这个案例利用JQuery来实现了一个小小的案例,这个案例很有制作的价值,拿来练手是非常不错滴~里面使用本地存储数据使得重新加载页面时,数据还是上次关闭页面的数据。
实现的功能有如下:
在输入框输入内容按下enter键可以将list加入到“正在进行”的内容中;
点击list内容前面的复选框可以将内容来回切换到“正在进行”和“已经完成”;
点击list内容后面的小圈可以将list删除;
分别统计“正在进行”和“已经完成”中list的个数,并显示出来;
点击list中的内容可以对里面的内容进行修改,并且修改的数据将重新保存到localstorage中然后加载到页面中。
网页的展示:
首先贴上我们的HTML代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>ToDoList—最简单的待办事项列表</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/index.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 © 2014 todolist.cn
</footer>
</body>
</html>
然后是我们的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:first-child {
position: absolute;
top: 2px;
left: 10px;
width: 22px;
height: 22px;
cursor: pointer;
}
li input:nth-child(2) {
position: absolute;
top: 2px;
left: 50px;
width: 80%;
height: 20px;
padding-left: 5px;
border-radius: 10px;
}
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;
}
}
接下来是每个功能的具体实现:
1.按下keydown键:首先获取本地存储的数据,然后将本次输入的数据通过数组的push()方法添加到数组中,然后将数据保存到本地存储,最后再将数据渲染到页面:
2.点击list右边的小圈删除list,这里的获取索引是新学到的一个小技巧:因为$.each(ele,function(index,item){})是可以遍历在同一个父亲祖先元素里的所有兄弟元素,比如一个ul里的li,但是如果需要获取遍历中的表兄弟元素的索引,则可以给这个相同的元素添加一个共同的自定义的属性作为标识,例如案例中的小li中的a标签元素,增加了一个id属性,则在给li中的a元素绑定事件时,可以获取这个属性对应索引来作为li的索引,进而实现删除li的操作。
$('#title').on('keydown', function(event) {
if (event.keyCode === 13) {
if ($(this).val() === "") {
alert('请输入内容哦~');
} else {
// 先读取本地存储原来的数据 原来有就获取,没有就创建一个空的数组
var local = getData();
// 然后获取本次输入到todolist表单的东西添加到数据里
local.push({ title: $(this).val(), done: false });
// 再然后将数据保存到本地存储
saveData(local);
// 将数据渲染到页面
load();
// 渲染后自己框里的内容清空
$(this).val("");
}
}
})
$('ol,ul').on('click', 'a', function() {
// 首先获取本地的数据
var data = getData();
// 获取点击的li的索引 给a标签加一个自定义的id属性为了获取点击的索引,因为每一个a在不同的li里
var index = $(this).attr('id');
// 删除点击的小li的数据 用数组的splice()方法
data.splice(index, 1);
// 再将删除后的数据保存到本地存储
saveData(data);
// 重新渲染数据到页面
load();
})
3.点击按钮后改变数据的done属性值
$('ol,ul').on('click', 'input:first-child', function() {
// 先获取本地存储的数据
var data = getData();
// 获取点击的索引
var index = $(this).siblings('a').attr('id');
// 修改数据
data[index].done = $(this).prop('checked');
// 将数据保存到本地存储
saveData(data);
// 重新渲染数据到页面
load();
})
4.修改list里的数据可以改变内容:
接下来就是封装实现功能的函数:
$('ol,ul').on('change', 'input:nth-child(2)', function() {
// 获取原来的数据
var data = getData();
// 获取点击的索引
var index = $(this).siblings('a').attr('id');
// 修改数据
data[index].title = $(this).val();
// 将数据保存到本地存储
saveData(data);
// 重新渲染数据到页面
load();
})
①获取本地存储的数据的函数:这个案例中数据存储的格式为[{title:内容,done:false/true},{title:内容,done:false/true},...]如果本地存储的数据不为空就返回获取的数据,如果为空则返回一个空的数组。使用localStorage.getItem()来获取本地的数据,因为本地存储的数据的字符串的形式,所以需要将其通过JSON.parse()转换为对象格式。
var getData = function() {
var data = localStorage.getItem('todolist');
if (data !== null) {
// 本地存储的数据是字符串格式,需转换为对象格式
return JSON.parse(data);
} else {
return [];
}
}
②保存数据到本地存储的函数:使用localStorage.setItem()将数据保存到本地存储,因为本地保存的数据是字符串的形式,所以需要通过JSON.stringify()来进行数据转换:将对象类型转换为字符串类型。
function saveData(data) {
localStorage.setItem('todolist', JSON.stringify(data));
}
③将数据渲染到页面的函数:在把新的数据渲染到页面之前需要对当前页面中的数据清空,不然它会再次渲染同样的内容后再渲染新增的内容,这里用到一个条件语句来进行判断,如果list数据中的done属性为真则说明list已经完成,将其放到“已经完成”中,如果done属性为假则放到“正在进行”中,还有一个统计数量的功能,每新增一条就+1,并将统计数据渲染到页面。
function load() {
// 首先获取数据
var data = getData();
// 统计数量
var todocount = 0;
var donecount = 0;
//添加之前先把ol,ul里的内容清空,不然会再次渲染之前的数据
$('ol,ul').empty();
$.each(data, function(index, item) {
if (item.done) {
$('ul').prepend('<li><input type="checkbox" checked="checked"><input type="text" value=' + item.title + '><a href="javascript:;" id=' + index + '></a></li>');
donecount++;
} else {
$('ol').prepend('<li><input type="checkbox"><input type="text" value=' + item.title + '><a href="javascript:;" id=' + index + '></a></li>');
todocount++;
}
})
$('#donecount').text(donecount);
$('#todocount').text(todocount);
}
到这里,这个案例就完成了,然后接下来要总结一下在这个案例里学到的东西:
最重要的是巩固了JQuery的学习,期待下一篇博客写JQuery的常用知识点。
然后就是本地存储:localStorage:它只能存储字符串的数据格式,所以在使用localStroage.setItem()来存储数据前需要通过JSON.stringify()来将数组对象转换为字符串格式;同时在使用localStroage.getItem()获取数据前需要通过JSON.parse()来将字符串数据转换为对象格式,才能在JS中使用。
还有一个就是学习到了数组中的splice(从哪个位置开始,删除多少个)方法用来删除数组中的元素
最后就是一个小技巧:如果需要获取表兄弟中相同元素的索引,可以为每个元素自定义一个属性来进行获取。
这次总结就到这里,每天都要元气满满呀~