JSFiddle - React, Tailwind, and code Playground
by wenwu
HTML
<div class ="container" >
<h1>待辦事項</h1>
<div class="input-div">
<input class="" id="input" type="text" placeholder="輸入代辦事項..." >
<button class="" onclick="add()">新增 </button>
</div>
<div class ="container">
<h2 class="todo-list-title" id="">TO DO LIST</h2>
<ul class="todo-list" id="todo-list"> </ul>
</div>
</div>
CSS
* {
box-sizing: border-box;
font-size: 20px;
color: gray;
}
body{
background-color: #eee;
}
.container {
border: 0px solid black;
width: 400px;
margin: 50px auto;
}
h1 {
text-align: center;
font-size: 2rem;
}
.input-div{
display: flex;
align-items: center;
margin: 10px 0;
}
.input-div > input{
flex: 1px;
}
h2 {
text-align: center;
border-bottom: 1px solid #ccc;
font-size: 20px;
color: #666;
border: 0px solid black;
}
.todo-list {
margin: 0;
padding: 0;
}
.todo-list li {
list-style-type: none;
text-align: left;
border: 0px solid black;
background-color: rgba(255, 255, 255, 0.7);
padding: 10px 5px;
margin-bottom: 8px;
}
JavaScript
function add(){
let input =document.getElementById('input').value;
let list = document.getElementById('todo-list');
let li = document.createElement('li');
let span = document.createElement('span');
if (input == ""){
alert("請輸入事項!");
}else{
list.append(li);
li.append(span);
span.textContent = input;
input.value = '';
}
}