當用戶單擊 ID 為 的按鈕時generate-todo,我想從 ID 的輸入中獲取值new-todo,并將輸入值存盤為新串列項的文本,并將該新項附加到具有類的無序串列中的todos。這是我迄今為止嘗試過的,但我錯過了一些東西。
let todo = document.querySelector('#generate-todo').addEventListener('click', function() {
todo = document.querySelector('#new-todo').value;
const create = document.createElement('li');
document.querySelector('.todos').append(create);
});
<label for="new-todo">Add a new item to the list:</label>
<input id="new-todo" type="text">
<button id="generate-todo">Add to the List!</button>
<h1>List of things to do</h1>
<ul class="todos">
</ul>
uj5u.com熱心網友回復:
這應該讓你開始
document.getElementById('generate-todo').addEventListener('click', function() {
let todo = document.getElementById('new-todo').value;
const li = document.createElement('li');
li.innerText = todo;
document.getElementById('todos').append(li);
});
<label for="new-todo">Add a new item to the list:</label>
<input id="new-todo" type="text">
<button id="generate-todo">Add to the List!</button>
<h1>List of things to do</h1>
<ul id="todos">
</ul>
uj5u.com熱心網友回復:
您缺少一個.intodos選擇器。
document.querySelector(".todos").append(create)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433091.html
標籤:javascript html dom
上一篇:解釋協程的有趣行為
