-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo2.html
88 lines (76 loc) · 2.72 KB
/
todo2.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
<html>
<head>
<title>To-Do List</title>
<script>
"use strict"
function createToDo() {
// 加入創建 To-Do 的標籤
var todo = document.createElement("div"); // To-Do div
var span = document.createElement("span"); // To-Do Span
var replaceButton = document.createElement("button"); // To-Do replace button
var removeButton = document.createElement("button"); // To-Do remove button
// 處理 To-Do 的文字內容
var input = document.getElementById("input").value;
if (input == "") {
input = "廢文一篇";
}
span.innerHTML = input;
// radio
if(document.getElementById("radio1").checked) {
span.style.color = "red";
}
else {
span.style.color = "green";
}
todo.appendChild(span);
// 設定 replaceButton onclick 事件
// 撰寫 replaceButton onclick 功能
replaceButton.onclick = function() {
var input = document.getElementById("input").value;
if (input == "") {
alert("你並沒有輸入任何文字");
return;
}
// this:「R」按鈕, parentNode:To-Do的div, firstChild:負責文字的span, innerHTML:span的文字內容
this.parentNode.firstChild.innerHTML = input;
document.getElementById("input").value = "";
}
// 設定 replaceButton 的文字,
replaceButton.textContent = "R";
// 把 replaceButton 加入 To-Do
todo.appendChild(replaceButton);
// 完成 removeButton 按鈕
removeButton.onclick = function() {
// https://www.w3schools.com/jsref/met_win_confirm.asp
var r = confirm("是否確定刪除?");
if(r == true) {
// this.parentNode.parentNode.removeChild(this.parentNode);
this.parentNode.style.color = "red";
this.parentNode.removeChild(replaceButton);
this.parentNode.removeChild(this);
}
}
removeButton.textContent = "V";
todo.appendChild(removeButton);
// 把 To-Do 加入 todolist
// var todolist = document.createElement("todolist");
document.getElementById("todolist").appendChild(todo);
// 清空輸入欄位
document.getElementById("input").value = "";
}
</script>
</head>
<body>
<input type="text" id="input">
<button onclick="createToDo()">+</button>
<form action="">
<label>
<input type="radio" name="emergency" id="radio1" checked>緊急
</label>
<label>
<input type="radio" name="emergency">不緊急
</label>
</form>
<div id="todolist"></div>
</body>
</html>