[英]How to add a new item to a to-do list?
I am coding a form and a part of it requires adding new items to a to-do list named 'list of works'.我正在编写一个表单,其中一部分需要将新项目添加到名为“作品列表”的待办事项列表中。 The issue seems to be in my JavaScript. I am a total beginner and I have done my best but it is not working.
问题似乎在我的 JavaScript 中。我是一个完全的初学者,我已经尽力了,但它不起作用。 I would appreciate any help.
我将不胜感激任何帮助。 Attached is the link to my code https://plnkr.co/edit/QznPAo7ucKBN8xJF .
附件是我的代码https://plnkr.co/edit/QznPAo7ucKBN8xJF的链接。
function newElement() { var li = document.createElement("li"); var inputValue = document.getElementById("myInput").value; var t = document.createTextNode(inputValue); li.appendChild(t); document.getElementById("myInput").value = ""; var span = document.createElement("SPAN"); var txt = document.createTextNode("×"); span.className = "close"; span.appendChild(txt); li.appendChild(span); }
/* the box containg the 'new project' form */.box { border: none; padding: 4%; background-color: #FAEC31; border-radius: 4%; } /* the styling of text in the form */.form-content { font-size: 13px; font-family: Helvetica; }.addBtn { padding: 1%; background-color: #FAD626; cursor: pointer; border-radius: 20%; border-color: #FAD626; }.addBtn:hover { background-color: #F2F6F8; }
<div class="box"> <div class="form-content"> <form> <h2>Project information</h2> <label for="works-list">List of works:</label> <div id="myDIV" class="header"> <input type="text" id="myInput" placeholder="Title..."> <span onclick="newElement()" class="addBtn">Add</span> </div> <ul id="myUL"> <li>to do 1</li> <li>to do 2</li> </ul> </form> </div> </div>
You forgot to add the item to the list.您忘记将项目添加到列表中。 Retrieve the
UL
element and append the LI
one to it:检索
UL
元素和 append LI
元素:
// [...]
li.appendChild(span);
// UL retrieval
var ul = document.getElementById("myUL");
// LI appending
ul.appendChild(li);
Working snippet:工作片段:
function newElement() { var li = document.createElement("li"); var inputValue = document.getElementById("myInput").value; var t = document.createTextNode(inputValue); li.appendChild(t); document.getElementById("myInput").value = ""; var span = document.createElement("SPAN"); var txt = document.createTextNode("×"); span.className = "close"; span.appendChild(txt); li.appendChild(span); var ul = document.getElementById("myUL"); ul.appendChild(li); }
/* the box containg the 'new project' form */.box { border: none; padding: 4%; background-color: #FAEC31; border-radius: 4%; } /* the styling of text in the form */.form-content { font-size: 13px; font-family: Helvetica; }.addBtn { padding: 1%; background-color: #FAD626; cursor: pointer; border-radius: 20%; border-color: #FAD626; }.addBtn:hover { background-color: #F2F6F8; }
<div class="box"> <div class="form-content"> <form> <h2>Project information</h2> <label for="works-list">List of works:</label> <div id="myDIV" class="header"> <input type="text" id="myInput" placeholder="Title..."> <span onclick="newElement()" class="addBtn">Add</span> </div> <ul id="myUL"> <li>to do 1</li> <li>to do 2</li> </ul> </form> </div> </div>
You are missing getting the list and adding items to it with what you already created.您缺少获取列表并使用已创建的内容向其中添加项目。 Also, in your online sample, you are using
<script>
tags within your .js and that's a mistake, just get rid of them.此外,在您的在线示例中,您在.js中使用了
<script>
标记,这是一个错误,只需删除它们即可。
function newElement() {
var list = document.getElementById('myUL');
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
list.appendChild(li);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.