我在尝试制作待办事项清单时遇到问题。

huangapple go评论50阅读模式
英文:

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(&#39;form&#39;);    
const buttonEl = document.querySelector(&#39;.todo-button&#39;);
const todoUl = document.querySelector(&#39;.list&#39;);

buttonEl.addEventListener(&#39;click&#39;, (addTodo)=&gt; {

});

function addTodo() {
    //add div with class of &#39;todo&#39;
    const todoDiv = document.createElement(&#39;div&#39;);
    todoDiv.classList.add(&#39;todo&#39;)

    //create li
    const todoLi = document.createElement(&#39;li&#39;);
    todoLi.innerText = inputEl.value;
    todoDiv.appendChild(todoLi);

    //create button
    const todoButton = document.createElement(&#39;button&#39;);
    todoButton.innerHTML = `&lt;i class=&quot;fa-sharp fa-solid fa-trash trash&quot;&gt;&lt;/i&gt;`;
    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

你应该将&lt;li&gt;元素添加到&lt;ul&gt;中,而不是&lt;div&gt;元素。

此外,你应该使用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 &lt;li&gt; elements to the &lt;ul&gt;, not &lt;div&gt; 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(&#39;form&#39;);
const todoUl = document.querySelector(&#39;.list&#39;);
const pendingSpan = document.querySelector(&#39;.pending&#39;);
const clearBtn = document.querySelector(&#39;.clear&#39;);

formEl.addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault();
  addTodo(e.target.elements.task.value.trim());
  e.target.reset();
  pendingSpan.textContent = todoUl.children.length;
});

todoUl.addEventListener(&#39;click&#39;, (e) =&gt; {
  if (e.target.classList.contains(&#39;trash&#39;)) {
    e.target.closest(&#39;.todo&#39;).remove();
    pendingSpan.textContent = todoUl.children.length;
  }
});

clearBtn.addEventListener(&#39;click&#39;, (e) =&gt; {
  todoUl.innerHTML = &#39;&#39;;
  pendingSpan.textContent = &#39;0&#39;;
});

function addTodo(text) {
  // Add LI with class of &#39;todo&#39;
  const todoLi = document.createElement(&#39;li&#39;);
  todoLi.classList.add(&#39;todo&#39;)

  // Create SPAN
  const todoSpan = document.createElement(&#39;span&#39;);
  todoSpan.innerText = text;
  todoLi.appendChild(todoSpan);

  // Create BUTTON
  const todoButton = document.createElement(&#39;button&#39;);
  todoButton.innerHTML = `&lt;i class=&quot;fa-sharp fa-solid fa-trash trash&quot;&gt;&lt;/i&gt;`;
  todoLi.appendChild(todoButton);

  // Append each LI to UL
  todoUl.appendChild(todoLi);
}

<!-- language: lang-css -->

.todo &gt; span {
  display: inline-block;
  min-width: 10em;
}

.todo &gt; button &gt; i.trash {
  color: red;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;h1&gt;Todo App&lt;/h1&gt;
&lt;form autocomplete=&quot;off&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;task&quot; id=&quot;task&quot; placeholder=&quot;Enter a task...&quot; required&gt;
  &lt;button type=&quot;submit&quot; class=&quot;todo-button&quot;&gt;&lt;i class=&quot;fa-solid fa-plus plus&quot;&gt;&lt;/i&gt;&lt;/button&gt;
&lt;/form&gt;
&lt;div class=&quot;todo-container&quot;&gt;
  &lt;ul class=&quot;list&quot;&gt;
    &lt;!--
      &lt;div class=&quot;todo&quot;&gt;
        &lt;li&gt;Create something&lt;/li&gt;
        &lt;button&gt;&lt;i class=&quot;fa-sharp fa-solid fa-trash trash&quot;&gt;&lt;/i&gt;&lt;/button&gt;
      &lt;/div&gt;
    --&gt;
  &lt;/ul&gt;
&lt;/div&gt;

&lt;div class=&quot;todo-pending&quot;&gt;
  &lt;p&gt;You have &lt;span class=&quot;pending&quot;&gt;0&lt;/span&gt; pending task(s)&lt;/p&gt;
  &lt;button class=&quot;clear&quot;&gt;Clear All&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月10日 21:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定