js实现todolist
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
border: 0;
outline: none;
list-style: none;
box-sizing: border-box;
}
body {
background-color: #f48c78;
overflow-x: hidden;
transform: scale(1.2);
}
#container {
width: 300px;
margin: 80px auto 0 auto;
padding: 10px;
min-height: 450px;
background-color: #2f3d4a;
box-shadow: 0px 3px 10px #8f5246, 3px 0px 10px #8f5246;
border-radius: 10px;
}
#container h1 {
color: #fcffff;
width: 85px;
margin: 30px auto;
}
#input {
font-size: 0;
position: relative;
}
#input input {
width: 80%;
height: 30px;
margin-left: 9%;
border-radius: 15px;
padding: 5px 10px;
}
#input input::-webkit-input-placeholder {
/* chrome safari*/
color: #afb2b9;
text-align: center;
}
#input input::-moz-placeholder {
/* 火狐获取placeholder*/
color: #afb2b9;
text-align: center;
}
#input button {
width: 45px;
height: 30px;
border-radius: 0 15px 15px 0;
background-color: #f48c78;
position: absolute;
right: 30px;
cursor: pointer;
opacity: 0;
transition: all 0.5s 0.1s;
visibility: hidden;
color: #fff;
}
#input input:focus + button,
#input button:hover {
opacity: 1;
visibility: visible;
}
#input button:hover {
background-color: #cd4f35;
}
ul {
margin: 30px 0;
}
ul li {
width: 224px;
margin: 10px 0 0 9%;
color: #fcffff;
background-color: #283140;
font-weight: 600;
padding: 5px 10px;
border-radius: 10px;
}
ul li:hover {
background-color: #455878;
transition: all 0.4s;
}
.removebtnContainer {
margin: 4px 5px 0 0;
float: right;
border-radius: 50%;
width: 13px;
height: 13px;
border: solid #fd8263;
position: relative;
top: 50%;
transition: all 0.2s;
cursor: pointer;
}
.removebtnContainer:hover {
background-color: #fd8263;
}
</style>
</head>
<body>
<!-- html部分属实简单 -->
<div id="container">
<h1>Todos</h1>
<div id="input">
<input type="text" id="content" placeholder="别玩了懒狗" onfocus="this.placeholder = ''" onblur="this.placeholder = '别玩了懒狗'" />
<!-- placeholder 内容自己改 -->
<button>add</button>
</div>
<ul id="ul"></ul>
</div>
<script>
let todoList = document.getElementsByTagName('ul')[0]
let inputElement = document.getElementById('content')
let buttonElement = document.getElementsByTagName('button')[0]
let todos = JSON.parse(localStorage.getItem('todos')) || [] // todos数组保存待办项
function addNew(content) {
let item = document.createElement('li')
item.innerText = content
addNewButton(item)
todoList.appendChild(item)
todos.push(content)
save()
}
function addNewButton(item) {
let container = document.createElement('div')
container.className = 'removebtnContainer'
item.appendChild(container)
}
function save() {
localStorage.setItem('todos', JSON.stringify(todos)) // 将数组转为字符串并储存在本地
}
function reLoad() {
todos.forEach(content => {
let item = document.createElement('li')
item.innerText = content
addNewButton(item)
todoList.appendChild(item)
})
}
function reMove(item) {
item.parentNode.remove()
todos.forEach((content, index) => {
if (item.parentNode.innerText.trim() == content) {
todos.splice(index, 1) // splice方法 删除index开始的 1项
}
})
save()
}
/* ↑ 函数声明
-------------------------------------------------------------------------
事件、执行 ↓ */
reLoad() //从本地载入
/*两种方式添加待办项*/
//鼠标点击添加
buttonElement.addEventListener(
'click',
() => {
if (inputElement.value) {
addNew(inputElement.value.trim())
inputElement.value = ''
}
},
false
)
//键盘回车添加
inputElement.addEventListener('keyup', event => {
console.log(event)
if (event.keyCode == 13 && inputElement.value) {
//回车 keyCode === 13
addNew(inputElement.value.trim())
inputElement.value = ''
}
})
//点击删除
todoList.addEventListener(
'click',
event => {
if (event.target.className == 'removebtnContainer') {
reMove(event.target)
}
},
false
)
</script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
编辑 (opens new window)
上次更新: 2021/10/23, 15:40:38