英文:
I am having problems when trying to make a to-do list
问题
我正在尝试创建一个JavaScript待办事项列表,但我遇到了问题:
const inputEl = document.querySelector('form');
const buttonEl = document.querySelector('.todo-button');
const todoUl = document.querySelector('.list');
buttonEl.addEventListener('click', (addTodo)=> {
});
function addTodo() {
//添加具有类名 'todo' 的 div
const todoDiv = document.createElement('div');
todoDiv.classList.add('todo')
//创建 li
const todoLi = document.createElement('li');
todoLi.innerText = inputEl.value;
todoDiv.appendChild(todoLi);
//创建按钮
const todoButton = document.createElement('button');
todoButton.innerHTML = `<i class="fa-sharp fa-solid fa-trash trash"></i>`;
todoDiv.appendChild(todoButton);
//将所有内容附加到 todoLi
todoUl.appendChild(todoDiv);
}
由于某种原因,它不起作用,我不知道为什么!
请帮助我,谢谢!
英文:
I am trying to create a javascript to-do list, but I am having a problem:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const inputEl = document.querySelector('form');
const buttonEl = document.querySelector('.todo-button');
const todoUl = document.querySelector('.list');
buttonEl.addEventListener('click', (addTodo)=> {
});
function addTodo() {
//add div with class of 'todo'
const todoDiv = document.createElement('div');
todoDiv.classList.add('todo')
//create li
const todoLi = document.createElement('li');
todoLi.innerText = inputEl.value;
todoDiv.appendChild(todoLi);
//create button
const todoButton = document.createElement('button');
todoButton.innerHTML = `<i class="fa-sharp fa-solid fa-trash trash"></i>`;
todoDiv.appendChild(todoButton);
//append all to todoLi
todoUl.appendChild(todoDiv);
}
<!-- end snippet -->
For some reason, it is not working and I do not know why!
Please help me, Thank you!
答案1
得分: 1
你应该将<li>
元素添加到<ul>
中,而不是<div>
元素。
此外,你应该使用submit
事件。这样,你可以通过在输入字段中按Enter键来触发添加操作。由于这是一个表单,你可以在提交时要求文本字段为必填项。表单输入的名称应该更具体一些;尝试使用task
而不是input
。
最后,addTodo
函数应该只接受文本字符串。
以下是你提供的代码的翻译部分:
const formEl = document.querySelector('form');
const todoUl = document.querySelector('.list');
const pendingSpan = document.querySelector('.pending');
const clearBtn = document.querySelector('.clear');
formEl.addEventListener('submit', (e) => {
e.preventDefault();
addTodo(e.target.elements.task.value.trim());
e.target.reset();
pendingSpan.textContent = todoUl.children.length;
});
todoUl.addEventListener('click', (e) => {
if (e.target.classList.contains('trash')) {
e.target.closest('.todo').remove();
pendingSpan.textContent = todoUl.children.length;
}
});
clearBtn.addEventListener('click', (e) => {
todoUl.innerHTML = '';
pendingSpan.textContent = '0';
});
function addTodo(text) {
// 添加带有类名'todo'的LI元素
const todoLi = document.createElement('li');
todoLi.classList.add('todo')
// 创建SPAN元素
const todoSpan = document.createElement('span');
todoSpan.innerText = text;
todoLi.appendChild(todoSpan);
// 创建按钮元素
const todoButton = document.createElement('button');
todoButton.innerHTML = `<i class="fa-sharp fa-solid fa-trash trash"></i>`;
todoLi.appendChild(todoButton);
// 将每个LI元素附加到UL元素
todoUl.appendChild(todoLi);
}
.todo > span {
display: inline-block;
min-width: 10em;
}
.todo > button > i.trash {
color: red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"/>
<h1>Todo App</h1>
<form autocomplete="off">
<input type="text" name="task" id="task" placeholder="输入任务..." required>
<button type="submit" class="todo-button"><i class="fa-solid fa-plus plus"></i></button>
</form>
<div class="todo-container">
<ul class="list">
<!--
<div class="todo">
<li>创建一些东西</li>
<button><i class="fa-sharp fa-solid fa-trash trash"></i></button>
</div>
-->
</ul>
</div>
<div class="todo-pending">
<p>你有<span class="pending">0</span>个待办任务</p>
<button class="clear">清除全部</button>
</div>
英文:
You should add <li>
elements to the <ul>
, not <div>
elements.
Also, you should use the submit
event. This way you can trigger an add by pressing enter in the input field. Since this is a form, you can make the text required upon submission. This gives you free form validation. The name of the form input should be more specific; try task
instead of input
.
Finally, the addTodo
function should only accept a text string.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const formEl = document.querySelector('form');
const todoUl = document.querySelector('.list');
const pendingSpan = document.querySelector('.pending');
const clearBtn = document.querySelector('.clear');
formEl.addEventListener('submit', (e) => {
e.preventDefault();
addTodo(e.target.elements.task.value.trim());
e.target.reset();
pendingSpan.textContent = todoUl.children.length;
});
todoUl.addEventListener('click', (e) => {
if (e.target.classList.contains('trash')) {
e.target.closest('.todo').remove();
pendingSpan.textContent = todoUl.children.length;
}
});
clearBtn.addEventListener('click', (e) => {
todoUl.innerHTML = '';
pendingSpan.textContent = '0';
});
function addTodo(text) {
// Add LI with class of 'todo'
const todoLi = document.createElement('li');
todoLi.classList.add('todo')
// Create SPAN
const todoSpan = document.createElement('span');
todoSpan.innerText = text;
todoLi.appendChild(todoSpan);
// Create BUTTON
const todoButton = document.createElement('button');
todoButton.innerHTML = `<i class="fa-sharp fa-solid fa-trash trash"></i>`;
todoLi.appendChild(todoButton);
// Append each LI to UL
todoUl.appendChild(todoLi);
}
<!-- language: lang-css -->
.todo > span {
display: inline-block;
min-width: 10em;
}
.todo > button > i.trash {
color: red;
}
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"/>
<h1>Todo App</h1>
<form autocomplete="off">
<input type="text" name="task" id="task" placeholder="Enter a task..." required>
<button type="submit" class="todo-button"><i class="fa-solid fa-plus plus"></i></button>
</form>
<div class="todo-container">
<ul class="list">
<!--
<div class="todo">
<li>Create something</li>
<button><i class="fa-sharp fa-solid fa-trash trash"></i></button>
</div>
-->
</ul>
</div>
<div class="todo-pending">
<p>You have <span class="pending">0</span> pending task(s)</p>
<button class="clear">Clear All</button>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论