課後作業-JavaScript 系列六:第2課 ── 認識陣列操作

06_js_array-splice.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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JavaScript 系列六:第3課 ── 認識匿名函式</title> <link rel="stylesheet" href="./06_js_array-splice.css"> </head> <body> <input type="text"> <button id="add_btn" onclick="add()">新增事項</button> <div id="root"> </div>
<script src="./06_js_array-splice.js"></script> </body> </html>
|
### 06_js_array-splice.js
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
| var todos = [];
function render_wrap(){ const body = document.querySelector('body'); const wrap = document.createElement('div'); wrap.className='wrap'; const h2 = document.createElement('h2'); h2.textContent="To Do List"; const hr = document.createElement('hr');
const input_todo = document.querySelector('input'); const add_btn = document.querySelector('#add_btn');
const root = document.querySelector('#root'); wrap.append(h2); wrap.append(hr); wrap.append(input_todo); wrap.append(add_btn); wrap.append(root);
body.append(wrap); input_todo.addEventListener('keydown', function(event) { if (event.key === 'Enter') { add(); } }); }
function add() { const input_todo = document.querySelector('input'); console.log(input_todo.value); let todo = input_todo.value.trim(); if(todo !== ''){ todos.push(todo); console.log(todos); render(); }else { input_todo.focus(); input_todo.placeholder = "新增待辦事項"; } input_todo.value = ''; }
function render(){ console.log('render....'); const root = document.querySelector('#root'); root.textContent=''; const div_container=document.createElement('div'); div_container.className='todos_container'; const ul=document.createElement('ul'); ul.className='totoList'; todos.forEach(function(item, index){ const li=document.createElement('li'); const span=document.createElement('span'); const delBtn=document.createElement('button'); delBtn.className="delBtn"; delBtn.textContent='刪除'; delBtn.onclick = () => { todos = todos.filter((_, i) => i !== index); render(); }; span.textContent=item; li.append(span); li.append(delBtn); ul.append(li); })
div_container.append(ul); root.append(div_container); };
window.onload=()=>{ render_wrap(); };
|
### 06_js_array-splice.css
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
| @import url('https://fonts.googleapis.com/css2?family=Kalam:wght@300;400;700&family=Playwrite+CU:wght@100..400&display=swap'); *{ padding: 0; margin: 0; }
.wrap{ margin: 30px auto; width: 800px; min-height: 600px; background-color: #a9c5d5; border-radius: 10px; } h2{ padding-top: 20px; padding-left: 30px; font-family: "Kalam", cursive; font-weight: 400; font-style: normal; font-size: 35px; color:#e8626b; }
input{ width: 500px; margin: 20px 20px 20px 30px; padding-left: 5px; font-size: 20px; line-height: 2; border: 1px solid #889fad; color:#000; background-color: #fff;
} input:hover, input:focus { outline: none; background-color: #eaeaa9; } #add_btn{ border: none; font-size: 18px; border-radius: 5px; background-color: #000; color: #fff; padding: 10px; } #add_btn:hover{ cursor: pointer; background-color: #17507b; } ul{ min-height: 400px; margin: 0 30px; padding: 5px; border-radius: 5px; background-color: #8ca8ba; box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.3); } li{ display: flex; justify-content: space-between; align-items: center; margin: 10px 20px; border-bottom: 1px dotted #28292b; font-size: 18px; color: #000; } .delBtn{ border: none; font-size: 15px; border-radius: 3px; background-color: #040404; color: #fff; padding: 10px; }
.delBtn:hover{ cursor: pointer; background-color: #b91919; }
|
作業取自: https://codelove.tw/@howtomakeaturn/post/Aq5V0x
by 站長阿川