JSFiddle - React, Tailwind, and code Playground
by birdie2019
HTML
<div class="container">
<h1>待辦事項</h1>
<div id="input-panel">
<input type="text" id="input" placeholder="請輸入待辦事項...">
<select id="event-cat" onchange="changeColor()">
<option value="none" selected disabled hidden>事項分類</option>
<option value="normal">一般</option>
<option value="important">重要</option>
<option value="urgent">緊急</option>
</select>
<button class="add-btn" onclick="add()" >+</button>
</div>
</div>
<div class="container">
<ul id="list">
</ul>
<button id="export-btn">匯出</button>
</div>
CSS
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-size: 10px;
color: #606060;
}
body {
background-color: #FFF0DF;
}
.container {
width: 400px;
margin: 30px auto;
}
/* input-panel */
h1 {
text-align: center;
font-size: 2.4rem;
letter-spacing: 3px;
}
#input-panel {
display: flex;
justify-content: center;
margin: 10px 0;
}
#input-panel > input {
flex: 1;
padding: 10px 8px;
border-radius: 5px 0 0 5px;
border: 0;
outline: 0;
font-size: 1.5rem;
letter-spacing: 1px;
}
.text-normal {
color: #6B8F71;
font-weight: 500;
}
.text-important {
color: #F49344;
font-weight: 500;
}
.text-urgent {
color: #BF1802;
font-weight: 500;
}
#event-cat {
border: 0;
border-left: 3px solid #fffaf5;
width: 100px;
font-size: 1.5rem;
letter-spacing: 1px;
outline: 0;
text-align: center;
}
.add-btn {
border: 0;
width: 40px;
border-radius: 0 5px 5px 0;
font-size: 1.8rem;
font-weight: 700;
cursor: pointer;
background-color: #546A76;
color: #fff;
}
/* list */
#list > li {
display: flex;
justify-content: space-between;
background-color: rgba(255, 255, 255, 0.8);
padding: 10px 8px;
margin-bottom: 8px;
border-radius: 5px;
}
#list > li > span {
font-size: 1.5rem;
font-weight: 500;
letter-spacing: 1px;
}
#list > li > button {
background-color: transparent;
border: 0;
cursor: pointer;
font-size: 1.8rem;
font-weight: 900;
transition: all 0.3s;
}
#list > li > button:hover {
color: #FA829C;
transform: scale(1.3);
}
/* export-btn */
#export-btn {
background-color: #8E6F64;
border: 0;
color: #fff;
border: 1px solid #AC8172;
padding: 4px 20px 4px 4px;
font-size: 1.5rem;
letter-spacing: 1px;
...
JavaScript
function add() {
var input = document.getElementById('input');
var list = document.getElementById('list');
var li = document.createElement('li');
var span = document.createElement('span');
var btn = document.createElement('button');
var eventCat = document.getElementById('event-cat');
var eventCatIndex = eventCat.selectedIndex;
var eventValue = eventCat.options[eventCatIndex].value;
var inputValue = input.value;
if (eventValue != 'none' && inputValue != '') {
list.append(li);
li.append(span);
span.className = eventValue;
li.append(btn);
btn.onclick = deleteLi;
btn.textContent = 'x';
span.textContent = inputValue;
input.value = '';
eventCat.value = 'none';
input.className = '';
} else {
alert('請填寫待辦事項和事件分類');
}
}
function deleteLi() {
var yes = confirm('確定要刪除嗎?');
if (yes) {
event.target.parentElement.remove();
}
}
var exportBtn = document.getElementById('export-btn');
exportBtn.onclick = exportList;
function exportList() {
var list = document.getElementById('list').children;
var text = '';
var num = 1;
for (var item of list) {
var getClassName = item.children[0].className;
if (getClassName == 'normal') {
text = text + num.toString() + '.' + ` ` + item.children[0].textContent + ` `;
} else if (getClassName == 'important') {
text = text + num.toString() + '.' + ` ` + '*' + item.children[0].textContent+ '*' + ` `;
} else {
text = text + num.toString() + '.' + ` ` + '**' + item.children[0].textContent+ '**' + ` `;
}
num = num + 1;
}
if (text != ''){
alert('今日待辦:' + text);
} else {
alert('今日無待辦事項');
}
}
function changeColor() {
var eventCatValue = event.target.options[event.target.selectedIndex].value;
console.log(eventCatValue);
var input = document.getElementById('input');
if (eventCatValue == 'normal') {
input.className = 'text-normal';
} else if (eventCatValue == 'important') {
input.className =...