简体   繁体   English

简单的ToDo-List不起作用

[英]Simple ToDo-List doesn't work

My ToDo List dont wanna work the way i want. 我的待办事项清单不想按我想要的方式工作。 I've just been working with JavaScript for 2 weeks sthis is very new to me, therefor the code maybe doesnt look that nice. 我刚刚使用JavaScript工作了两个星期这对我来说很新,因此代码可能看起来不太好看。

The result comes out wrong. 结果出错了。 If I type in "buy food" the first line gonna show just that, but the next time I wanna add "walk the dog", then it displays 如果我输入“购买食物”,第一行就会显示,但下次我想添加“遛狗”,然后显示

  • buy food 买食物
  • buy food 买食物
  • walk the dog 遛狗

I hope you understand my problem. 我希望你理解我的问题。 It also ends the unordered list tag after the first click and adds the rest of the things in another. 它还会在第一次单击后结束无序列表标记,并将其余内容添加到另一个中。

Here's the JavaScript: 这是JavaScript:

var taskList = [];

var text = "<ul>"

function addToList() {
    var task = document.getElementById("toDoTask").value;
    taskList.push(task);

    for(i = 0; i < taskList.length; i++){
        text += "<li>" + taskList[i] + "</li>" ;
    }

    text += "</ul>";

    document.getElementById("todoList").innerHTML = text;
}

The issue is you're closing the ul tag after adding each item. 问题是您在添加每个项目后关闭了ul标记。 Instead of concatenating raw HTML, consider using element objects and appending, and using a text node object to handle the user input - this removes the possibility of a DOM Based XSS vulnerability. 而不是连接原始HTML,考虑使用元素对象和追加,并使用文本节点对象来处理用户输入 - 这消除了基于DOM的XSS漏洞的可能性。

 window.onload = function() { var taskList = []; var container = document.getElementById("todoList"); document.getElementById("add").onclick = addToList; function addToList() { var task = document.getElementById("toDoTask").value; taskList.push(task); var ul = document.createElement('ul'); var li; for (i = 0; i < taskList.length; i++) { li = document.createElement('li'); li.appendChild(document.createTextNode(taskList[i])) ul.appendChild(li); } container.innerHTML = ''; container.appendChild(ul); } }; 
 Task: <input id="toDoTask" /> <input type="button" id="add" value="Add" /> <div id="todoList"> </div> 

On each call of function addToList() you should reset the variable text . 在函数addToList()每次调用中,您应该重置变量text

For example: 例如:

function addToList() {
  var task = document.getElementById("toDoTask").value;
  taskList.push(task);

  text="";

  for(i = 0; i < taskList.length; i++){
      text += "<li>" + taskList[i] + "</li>" ;
  }

  text += "</ul>";

  document.getElementById("todoList").innerHTML = text;
}

The whole list of items in array will appends to variable text on each call. 数组中的整个项目列表将附加到每次调用的变量text

You should not use the innerHtml. 你不应该使用innerHtml。 This replace all the text of your content. 这将替换您内容的所有文本。 You should just add the li to your ul. 你应该把li添加到你的ul。 You can do that by using the append function by jquery Append 您可以通过使用jquery Append的append函数来实现

your <ul> must contain an id like this <ul id="toDoList"> 你的<ul>必须包含这样的id <ul id="toDoList">

then you make $("#toDoList").append("yourTask") ; 然后你做$("#toDoList").append("yourTask") ;

yourTask must contains the li. yourTask必须包含li。

With this, you don't need to iterate on all your element list 有了这个,您不需要迭代所有元素列表

不确定,但你似乎第二次不断添加text了,所以文字就像<ul><li>buy food</li></ul><li>buy food</li><li>walk the dog</li></ul> ,顺便说一下,它是无效的HTML,但无论如何都要输出...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 待办事项列表按钮不起作用 - Javascript button for todo list doesn't work 带有 useReducer 和 MongoDB 的待办事项列表 - 错误:“todo.map 不是函数” - Todo-List with useReducer and MongoDB - Error: „todo.map is not a function“ 待办事项列表应用程序 - ReactJS 上的 onChange 不起作用? - todo list app - onChange on ReactJS doesn't work? 无法从 React 中的待办事项列表中删除项目 - Cannot delete Item from Todo-list in React REACT Todo-List:如何检查数组中是否已经存在一项 - REACT Todo-List : How to check if an item already exist in the array 使用香草 javascript 删除待办事项列表中的父项,包括子项 - delete parent item including child on a todo-list with vanilla javascript 无法使用 react 和 redux 在待办事项列表中添加待办事项。 我究竟做错了什么? - Not able to add todo in a todo-list using react and redux. What am I doing wrong? 过滤任务不起作用 - 使用 es6 类的 MVC 模式的待办事项列表 - Filter tasks doesn't work - Todo list with MVC pattern using es6 classes TODO-list:如何在服务器响应之前立即在列表中添加项目? - TODO-list: How to add items on the list immediately, before server responded? 如何将按钮添加到待办事项列表以将任务标记为已完成 Javascript? - How can I add a button to a todo-list to mark tasks as done Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM