JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./index.css">
<title>Document</title>
</head>
<body>
<div id="h1Block">
<h1>TO-DO-LIST</h1>
</div>
<div id="inputBlock">
<div class="input-area">
<input type="text" id="input" placeholder="請輸入待辦事項...">
<br>
<button onclick="inputBtn()" class="inputBtn">Submit</button>
<button onclick="exportBtn()" class="exportBtn">Export</button>
<select onchange="changeInputColor()" name="" id="priorSelect">
<option id="" value="normal">Normal</option>
<option id="important" value="important">Important</option>
<option id="urgent" value="urgent">Urgent</option>
</select>
</div>
</div>
<hr>
<div id="outputBlock">
<ul id="list">
</ul>
</div>
<script src="./index.js"></script>
</body>
</html>
CSS
body {
background-color: darkslategray;
/* background-repeat: none; */
background-size: cover;
}
#h1Block {
color: whitesmoke;
font-size: larger;
font-family: Fantasy;
font-weight: bolder;
/* font-style: italic; */
text-decoration: underline;
text-align: center;
}
#inputBlock {
margin-top: 10px;
margin-bottom: 10px;
text-align: center;
}
#input {
width: 35%;
height: 32px;
font-size: 24px;
border-radius: 10px;
}
.redFontClass {
color: red;
}
.orangeFontClass {
color: orange;
}
.blackFontClass {
color: black;
}
.inputBtn {
border: 0px;
font-size: 24px;
color: black;
border: 3px solid darkred;
border-radius: 8px;
margin: 20px;
}
.inputBtn:hover {
cursor: pointer;
background-color: darkred;
color: white;
}
.exportBtn {
border: 0px;
font-size: 24px;
color: black;
border: 3px solid blueviolet;
border-radius: 8px;
margin: 20px;
}
.exportBtn:hover {
cursor: pointer;
background-color: blueviolet;
color: white;
}
#outputBlock {
font-size: 24px;
color: white;
display: block;
width: 36%;
margin-left: 30%;
margin-right: 10%;
align-items: center;
}
#list {
list-style-type: none;
}
#list li {
padding: 5px;
border-bottom: 1px dashed whitesmoke;
display: flex;
}
#list span {
width: 90%;
}
#list button {
border-radius: 5px;
border: 0 px;
width: 20%;
border: 3px solid black;
}
#list button:hover {
cursor: pointer;
background-color: rgb(36, 35, 35);
color: white;
}
/* 新增事件急迫性分類功能 */
#priorSelect {
font-size: 24px;
margin: 20px;
border: 3px solid black;
border-radius: 5px;
}
.important {
color: orange;
}
.urgent {
color: red;
}
#important {
background-color: orange;
}
#urgent {
background-color: red;
}
JavaScript
function inputBtn() {
const listText = document.getElementById("input").value;
var list = document.getElementById("list");
var elemSpan = document.createElement('span');
var elemLi = document.createElement('li');
var elemBtn = document.createElement('button')
let prior = document.getElementById('priorSelect');
let index = prior.selectedIndex;
let priorText = prior.options[index].text;
elemSpan.textContent = listText;
elemBtn.textContent = "Del";
list.append(elemLi);
elemLi.append(elemSpan);
elemLi.append(elemBtn);
if (priorText == "Important") {
elemSpan.className = 'important';
}
if (priorText == "Urgent") {
elemSpan.className = 'urgent';
}
elemBtn.onclick = delBtn;
}
function delBtn() {
event.target.parentElement.remove();
}
function exportBtn() {
let listText = document.getElementById('list').children;
let exportText = "待辦事項: ";
let num = 1;
for (var x of listText) {
if (x.children[0].className == 'important') {
exportText = exportText + '(' + num.toString() + ')*' + x.children[0].textContent + "* ";
} else if (x.children[0].className == 'urgent') {
exportText = exportText + '(' + num.toString() + ')**' + x.children[0].textContent + "** ";
} else {
exportText = exportText + '(' + num.toString() + ')' + x.children[0].textContent + " ";
}
num = num + 1;
}
alert(exportText);
}
function changeInputColor() {
let elemInput = document.getElementById('input');
elemInput.classList.remove('orangeFontClass');
elemInput.classList.remove('redFontClass');
elemInput.classList.remove('blackFontClass');
switch (event.target.value) {
case 'important':
elemInput.className = elemInput.className + 'orangeFontClass';
break;
case 'urgent':
elemInput.className = elemInput.className + 'redFontClass';
...