简体   繁体   English

我不知道我做错了什么,它不起作用,我正在尝试列出任务清单

[英]i don't know what i did wrong, it doesn't work, i'm trying to make a task list

<!DOCTYPE html> <!--Declare the document type and version of HTML-->
<html> 
    <head><!--Information related to the decument-->
        <title>Task List</title>   
        <link rel="stylesheet" href="mydesign.css">
    </head>
    <body><!--Information related to display the decument-->
        [<img src="cm1XiuT.png" alt="CounterStrike" style="width:100%;height:250pt;">][1]
        <input type="text" placeholder="Add Tasks Here..." id="taskValue">
        <button id="addBtn" onclick="addTask()">add</button>
        <!--horizontal Rule-->
        <hr>

    <section id="content">
    </section>
    <script>

/ creates an Array variable tasks checks if there are any stored tasks in the browser Conditional statement - Checks IF there is data stored then imports the data to the array IF not - exitx the function with an empty array IF THERE IS - exits the function with the poplulated array / / 创建一个Array变量任务检查浏览器中是否有任何存储的任务条件语句-检查是否存储了数据,如果没有则将数据导入到阵列中-如果不是-用空数组退出函数IF THERE IS-用-退出函数填充数组 /

this is a code 这是一个代码

function creatArray()
            {
                var taskArray = [];
                var tasks = localStorage.getItem("itemList");
                if(tasks != null)
                {
                /*code runs if the condition is met*/
                taskArray = JSON.parse(tasks);
            }
            return taskArray;
        }

/*Addsa task to the list Creates an array Creates a variable to store the information in the input fieldset clears the information in the input field Pushes the task into our Array Stores the updated Tasklist in the browser Calls the displayTask Function */ / *将任务添加到列表中创建一个数组创建一个变量以将信息存储在输入字段集中清除输入字段中的信息将任务推送到数组中将更新的Tasklist存储在浏览器中调用displayTask函数* /

function addTask()
            {
                var taskList = creatArray();
                var task = document.getElementById("taskValue").value;
                document.getElementById("taskValue").value = "";
                taskList.push(task);
                localStorage.setItem("itemList",JSON.stringify(taskList));
                displayTask();
            }

/*Removes a task from the list creats a variable to store the correct button information. / *从列表中删除任务会创建一个变量来存储正确的按钮信息。 this - as in "this"button that has been clicked creats an array removes the task from our array and defines how many items we need to remove calls the displayTask function */ this-如单击的“ this”按钮那样,创建一个数组将从数组中删除该任务,并定义需要删除的项目数,调用displayTask函数* /

function removeTask()
            {
            //remove the tasks from the array
            var rID = this.getAttribute("id");
            var taskList = createArray();
            taskList.splice(rID, 1);
            localStorage.setItem("itemList",JSON,stringify(taskList));
            displayTask();
            }

/*displays Tasks in the list Creates the array of tasks creates the variable to store the list items LOOP statement - adds HTML to each list item in the array {repeats until the end of the } replaces the content in the section tag with id="content" creates a button array LOOP STATMENT - adds an EvenListenerr to each button in the List */ / * displays列表中的任务创建任务数组创建变量以存储列表项LOOP语句-将HTML添加到数组中的每个列表项{重复,直到}的结尾用id =替换section标记中的内容“内容”创建一个按钮数组LOOP STATMENT-向列表中的每个按钮添加一个EvenListenerr * /

function displayTask()
            {
                var taskList = createArray();
                var showTasks = "<ul>";

                for(var i=0; i < taskLIst.length; i++)
                {
                showTasks += "<li>"+taskList[i]="<button class='rmvBtn'id='"+i+"'>remove</button></li>"
            }


showTasks += "</ul>";

                document.getElementById("content"),innerHTML =showTasks;

                var btnArray = document.getElementById("rmvBtn");
                for(i =0; i < btnArray.length; 1++)
                {
                btnArray[i].addEventListener('click',removeTask);
                }
            }

            displayTask()
        </script><!--includes an external javascript file-->
    </body>
</html>

Look, only javascript and html 看,只有javascript和html

HTML 的HTML

  <div id="tasks"></div>
  <div>
     <input type="text" id="newTaskInput">
     <button onclick="add()">Add</button>
  </div>

JavaScript 的JavaScript

var tasks = []

function init() {
  tasks = load()
  renderTasks()
}

init()

function renderTasks() {
   var container = document.getElementById('tasks')
   var frag = document.createDocumentFragment()

   tasks.forEach(function(item, i) {
   var div = document.createElement('div')
   var text = document.createTextNode(item.name)
   div.appendChild(text)

   var closeBtn = document.createElement('button')
   var btnText = document.createTextNode('x')
   closeBtn.appendChild(btnText)
   closeBtn.onclick = function() {
     remove(i)
   }
   div.appendChild(closeBtn)

   frag.appendChild(div)
 })

 container.innerHTML = ""
 container.appendChild(frag)
}

function add() {
  var input = document.getElementById('newTaskInput')
  tasks.push({ name: input.value })
  renderTasks()
  save(tasks)
}

function remove(index) {
  tasks.splice(index, 1)
  renderTasks()
  save(tasks)
}


function save(data) {
  localStorage.setItem('tasks', JSON.stringify(data))
}

function load() {
  var raw = localStorage.getItem('tasks')
  if ( raw ) {
    return JSON.parse(raw)
  }
  return []
} 

DEMO 演示

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

相关问题 我的Javascript不起作用。不知道它是函数,我是如何调用它,我使用的CSS或者是什么 - My Javascript doesn't work. Don't know if it's the Function, how I'm calling it, the CSS I used or what JavaScript错误:未捕获的TypeError:undefined不是第4行上的函数; 我不知道我做错了什么 - JavaScript error: Uncaught TypeError: undefined is not a function on line 4; I Don't Know What I Did Wrong Chrome扩展程序。 新的,不知道我做错了什么 - Chrome extension. New and don't know what i did wrong jQuery无法在html中工作,我不知道自己在做什么错 - jQuery not working in html and I don't know what i'm doing wrong 我无法从属性中提取XML数据,我不知道我做错了什么 - I'm having trouble extracting XML data from attributes and I don't know what I'm doing wrong 我不知道我做错了什么,但是我的带有 js 和 css 的 .html 不会显示必须显示的内容 - I don't know what I'm doing wrong but my .html with js and css won't show what is has to show 当我点击按钮时,我不知道怎么了 - I don't know what's wrong when i click the button nothing work 我的for循环不起作用,我也不知道为什么 - my for loop doesn't work and I don't know why 我不知道为什么这个“这个”方法不起作用? - I don't know why this 'this' method doesn't work? 我不知道为什么渲染在反应中不起作用 - I don't know why rendering doesn't work in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM