Skip to content

Commit 3b5c7e8

Browse files
authored
todolist
todolist
1 parent c89cb5a commit 3b5c7e8

3 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>localstorage本地存储</title>
8+
<link rel="stylesheet" href="./main.css">
9+
<script src="./main.js"></script>
10+
</head>
11+
<body>
12+
<div class="wrapper">
13+
<h2>我是前端切图仔</h2>
14+
<p></p>
15+
<ul class="plates">
16+
<li>待添加事项</li>
17+
</ul>
18+
<form class="add-items">
19+
<input type="text" name="item" placeholder="Item Name" required>
20+
<input type="submit" value="添 加">
21+
</form>
22+
<div class="buttons">
23+
<button data-action="clear">删除所有</button>
24+
<button data-action="check">全部选中</button>
25+
<button data-action="uncheck">取消</button>
26+
</div>
27+
</div>
28+
</body>
29+
</html>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
html {
2+
min-height: 100vh;
3+
display: flex;
4+
justify-content: center;
5+
align-items: center;
6+
}
7+
8+
.wrapper {
9+
padding: 20px;
10+
max-width: 350px;
11+
background: rgba(228, 215, 215, 0.95);
12+
box-shadow: 0 0 0 5px rgba(187, 157, 157, 0.5);
13+
}
14+
h2 {
15+
text-align: center;
16+
margin: 0;
17+
font-weight: 200;
18+
}
19+
.plates {
20+
margin: 0;
21+
padding: 0;
22+
text-align: left;
23+
list-style: none;
24+
}
25+
.plates li {
26+
border-bottom: 1px solid rgba(0,0,0,0.2);
27+
padding: 10px 0;
28+
font-weight: 100;
29+
display: flex;
30+
}
31+
.plates label {
32+
flex:1;
33+
cursor: pointer;
34+
}
35+
.plates input {
36+
display: none;
37+
}
38+
.plates input + label:before {
39+
content: '⬜️';
40+
margin-right: 10px;
41+
}
42+
.plates input:checked + label:before {
43+
content: '☆';
44+
}
45+
.add-items {
46+
margin-top: 20px;
47+
}
48+
.add-items input {
49+
padding:10px;
50+
outline:0;
51+
border:1px solid rgba(0,0,0,0.1);
52+
}
53+
.add-items input:nth-child(1){
54+
width: 61.3%;
55+
}
56+
.add-items input:nth-child(2) {
57+
width: 30%;
58+
color: rgb(85, 108, 128);
59+
font-weight: 700;
60+
}
61+
.buttons button {
62+
width: 100px;
63+
margin-top: 10px;
64+
height: 40px;
65+
color: rgb(85, 108, 128);
66+
font-weight: 700;
67+
border:1px solid rgba(0,0,0,0.1);
68+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
(function(){
2+
3+
function newFun() {
4+
5+
var addItems = document.querySelector('.add-items');//选中类为.add-items的元素
6+
var itemsList = document.querySelector('.plates');//todolist列表
7+
var buttons = document.querySelector('.buttons');
8+
var items = JSON.parse(localStorage.getItem('items')) || [];//获取本地缓存到的所有item,将一个对象字符串转换为对象
9+
10+
//添加item方法
11+
function handleSubmit(e) {
12+
e.preventDefault();//阻止默认事件的触发,防止在提交后页面自己刷新
13+
var name = this.querySelector('[name=item]').value;//获取输入框中的值
14+
15+
var item = {
16+
name: name,
17+
done: false//增加一个状态bool,后面会用到
18+
};
19+
items.push(item);
20+
localStorage.setItem('items', JSON.stringify(items));//将对象转化为字符串,因为本地存储只能以字符串的形式存储
21+
updateList( items, itemsList);//更新列表
22+
this.reset();
23+
}
24+
25+
function updateList(plates = [], plateList) {
26+
plateList.innerHTML = plates.map( function(plate, i) {
27+
28+
return '<li><input type="checkbox" data-index="' + i + '" id="plate' + i + '" ' + (plate.done ? 'checked' : '') + ' /><label for="plate' + i + '">' + plate.name + '</label></li>';}).join('');
29+
}
30+
31+
32+
//事件委托
33+
// 此处使用到了事件委托:
34+
// 假设我们队一个input列表进行了事件监听,但我们如法保证,此列表在接下来的状态下是否进行了更新,刷新等改变原来节点的操作,如果有这样的操作出现,那么我们之前的事件监听器就无法再起到监听的作用;
35+
// 但我们可以对input列表的父元素进行事件监听,让它们的父元素处于监听状态,当我们所点击的元素是其子元素的话,就告诉它的子元素,执行相应的事件;
36+
// 相当于委托父元素帮我们监听所有子元素,这样无论子元素列表进行怎么样的更新,改变,只要父元素节点不发生改变就可以持续起到监听的 作用。
37+
// 通过e.target.matches('input')可以判断所点击的元素是不是input元素,e.target返回所点击的宿主元素。
38+
// 通过获取到所点击的列表的序号,更改其done属性,更新后进行存储,就可以实现完成状态的事件。
39+
function toggleChecked(e) {
40+
if (!e.target.matches('input')) return;
41+
var item = e.target.dataset.index;
42+
items[item].done = !items[item].done;
43+
localStorage.setItem('items', JSON.stringify(items));
44+
updateList( items, itemsList);
45+
}
46+
47+
48+
//添加button事件
49+
function doButtonPress(e) {
50+
var action = e.target.dataset.action;
51+
switch (action) {
52+
case "clear":
53+
items = [];
54+
break;
55+
case "check":
56+
items.map( function(item) {
57+
return item.done = true;
58+
} );
59+
break;
60+
case "uncheck":
61+
items.map( function(item) {
62+
return item.done = false ;
63+
} );
64+
break;
65+
default:
66+
return;
67+
}
68+
localStorage.setItem('items', JSON.stringify(items));
69+
updateList( items, itemsList);
70+
}
71+
72+
addItems.addEventListener('submit', handleSubmit);
73+
itemsList.addEventListener('click', toggleChecked);
74+
buttons.addEventListener('click', doButtonPress);
75+
76+
updateList(items, itemsList);
77+
78+
}
79+
80+
document.addEventListener('DOMContentLoaded', newFun);
81+
//当初始的 HTML 文档被完全加载和解析完成之后,DOMContentLoaded 事件被触发,而无需等待样式表、图像和子框架的完成加载。另一个不同的事件 load 应该仅用于检测一个完全加载的页面。 在使用 DOMContentLoaded 更加合适的情况下使用 load 是一个令人难以置信的流行的错误,所以要谨慎。
82+
//注意:DOMContentLoaded 事件必须等待其所属script之前的样式表加载解析完成才会触发。
83+
84+
}());

0 commit comments

Comments
 (0)