JSFiddle - React, Tailwind, and code Playground
by evon0306
HTML
<script src="https://kit.fontawesome.com/41e2a8dba7.js" crossorigin="anonymous"></script>
<body>
<h1>To Do List</h1>
<div id="inputfield"><input type="text" id="input"><button onclick="add()">新增</button>
<button id="output" onclick="output()">匯出</button>
</div>
<div id="todo">
<ul id="todoul">
</ul>
</div>
</body>
CSS
* {
list-style: none;
margin: 0;
}
body {
background-color: rgb(255, 245, 233);
}
body h1 {
border: 5px rgb(255, 134, 48) solid;
background-color: rgba(230, 153, 65, 0.391);
border-top: none;
border-right: none;
border-radius: 20px;
padding: 1.5rem;
margin: 1.5rem;
width: 60%;
min-width: 170px;
}
body div#inputfield {
width: 60%;
margin: 0 1.5rem;
padding: 10px;
display: flex;
}
body div#inputfield input {
height: 30px;
width: 80%;
min-width: 150px;
border-radius: 5px;
margin-right: 10px;
font-size: 16px;
border: 1px dashed rgb(255, 134, 48);
}
body div#inputfield button {
width: 80px;
min-width: 50px;
font-size: 15px;
margin-right: 0.25rem;
border-radius: 10px;
background-color: rgba(230, 153, 65, 0.391);
border: 2px dashed rgb(255, 134, 48);
transition: all 0.2s ease;
}
body div#inputfield button:hover {
background-color: rgba(240, 183, 117, 0.753);
border: 2px dashed rgb(255, 134, 48);
cursor: pointer;
}
body div#todo ul {
padding: 0 10px;
margin: 0 1.5rem;
}
body div#todo ul li {
display: flex;
justify-content: space-between;
align-items: center;
width: 60%;
min-width: 170px;
margin: 8px 0;
padding: 15px;
font-size: 16px;
border-bottom: 2px dashed rgb(255, 134, 48);
}
body div#todo ul li input {
margin: 0 0.5rem;
cursor: pointer;
}
body div#todo ul li i {
cursor: pointer;
font-size: 1.2rem;
transition: all 0.3s ease;
}
body div#todo ul li i:hover {
color: rgb(255, 134, 48);
transform: scale(1.3);
}
JavaScript
function add(){
let todoText = document.getElementById('input').value; //取得input事項的值
if(todoText=="")return;
let ul = document.getElementById('todoul'); //選取
let list = document.createElement('li'); //新增一個li
let checkbox = document.createElement('input'); //新增一個input
checkbox.type = "checkbox"; //input的種類是勾勾
let span1 = document.createElement('span'); //新增等等要放事項的span
let trush = document.createElement('i'); //新增垃圾桶圖片
trush.className = "fa-solid fa-trash";
trush.onclick = deleteli;
span1.textContent = todoText;//剛剛取得的input事項放進去
//一一把element放到正確的位置
ul.append(list);
list.append(checkbox,span1,trush);
document.getElementById('input').value = "";//把input清空
}
function deleteli(){
event.target.parentElement.remove();
}
function output(){
let todoul = document.getElementById('todoul').children;
let num = 1;
let list = '';
for(let x of todoul){
list = list + '( ' + num.toString() + ' ) ' + x.children[1].textContent;
num = num + 1;
}
alert('今日待辦:' + list)
}