@@ -11,20 +11,18 @@ import todoVideo from "./todo.mp4";
1111- ToDoの追加ができます。
1212- ToDoの削除ができます。
1313- ToDoの編集ができます。
14- - 入力欄が空欄だとToDoの追加ができなくなります。
1514
1615<video src = { todoVideo } controls muted />
1716
1817## 手順
1918
20- いきなり作るのが難しい場合はタスクを分解してみましょう。今回はルールにある4つの課題をひとつずつ解決していきます 。
19+ いきなり作るのが難しい場合はタスクを分解してみましょう。今回はルールにある3つの課題をひとつずつ解決していきます 。
2120
22211 . ToDoを追加できるようにする。
23222 . ToDoを削除できるようにする。
24233 . ToDoを編集できるようにする。
25- 4 . 空のToDoを追加できないようにする。
2624
27- の4つの仕事があるので 、まず1からやっていきましょう。
25+ の3つの仕事があるので 、まず1からやっていきましょう。
2826
2927### ステップ1: ToDoを追加する
3028
@@ -222,70 +220,3 @@ addButton.onclick = () => {
222220<ViewSource url = { import .meta .url } path = " _samples/step3" />
223221
224222</Answer >
225-
226- ### ステップ4 (発展) : 空のToDoを入れさせない
227-
228- [ ` HTMLButtonElement#disabled ` プロパティ] ( https://developer.mozilla.org/ja/docs/Web/API/HTMLButtonElement/disabled ) が` true ` の時、ボタンはクリックを受け付けなくなります。入力欄が空の時にこのプロパティを` true ` に、それ以外の時は` false ` にすることによって空のタスクの追加を防ぐことができます。この時、入力欄に何かキー入力があるたびに入力欄が空かどうかを判定する必要があります。[ ` HTMLElement#oninput ` プロパティ] ( https://html.spec.whatwg.org/multipage/webappapis.html#handler-oninput ) にイベントハンドラを登録すると、ユーザーによって要素に入力されたときに実行される関数を定めることができます。
229-
230- ``` javascript
231- todoInput .oninput = () => {
232- // 入力欄が空の時はボタンを押せないようにする
233- addButton .disabled = todoInput .value === " " ;
234- };
235- ```
236-
237- <Answer title = " ステップ4" >
238-
239- ``` html title="index.html"
240- <!doctype html>
241- <html lang =" ja" >
242- <head >
243- <meta charset =" utf-8" />
244- <title >Title</title >
245- </head >
246- <body >
247- <ul id =" todo-list" ></ul >
248- <input id =" todo-input" />
249- <button id =" add-button" disabled >追加</button >
250- <script src =" ./script.js" ></script >
251- </body >
252- </html >
253- ```
254-
255- ``` javascript title="script.js"
256- const todoList = document .getElementById (" todo-list" );
257- const todoInput = document .getElementById (" todo-input" );
258- const addButton = document .getElementById (" add-button" );
259-
260- todoInput .oninput = () => {
261- addButton .disabled = todoInput .value === " " ;
262- };
263-
264- addButton .onclick = () => {
265- const todoItem = document .createElement (" li" );
266- const todoText = document .createElement (" span" );
267- const editButton = document .createElement (" button" );
268- const deleteButton = document .createElement (" button" );
269- todoText .textContent = todoInput .value ;
270- todoInput .value = " " ;
271- addButton .disabled = true ; // valueへの代入はoninputイベントを発火しない
272- editButton .textContent = " 編集" ;
273- editButton .onclick = () => {
274- const input = prompt (" 新しい内容を入力してください。" );
275- // prompt関数は入力された文字列が空の場合は空文字列 ("")、キャンセルされた場合はnullを返す
276- if (input !== " " && input !== null ) todoText .textContent = input;
277- };
278- deleteButton .textContent = " 削除" ;
279- deleteButton .onclick = () => {
280- todoList .removeChild (todoItem);
281- };
282- todoItem .appendChild (todoText);
283- todoItem .appendChild (editButton);
284- todoItem .appendChild (deleteButton);
285- todoList .appendChild (todoItem);
286- };
287- ```
288-
289- <ViewSource url = { import .meta .url } path = " _samples/todo" />
290-
291- </Answer >
0 commit comments