简体   繁体   English

如何在JavaScript中添加“删除所有按钮”

[英]How to add a 'delete all button' in javascript

I'm new with javascript and i try to make a to do list for the first time. 我是javascript的新手,我第一次尝试制作待办事项清单。 I'm trying to add a button to delete all of the tasks in a to do list. 我正在尝试添加一个按钮,以删除待办事项列表中的所有任务。 I can't find how it works. 我找不到它的工作原理。

var inputField = document.getElementById("inputField");
inputField.focus(); 
inputField.onkeyup = function (event) {

  if (event.which === 13) { 
    var taak = inputField.value;

    if (inputField.value.length === 0 || inputField.value == " ") {
      return false;
    }

    addNewItem(document.getElementById("todoList"), taak); 

    inputField.focus();
    inputField.select();
  }
};


function addNewItem(list, taak) {
  var date = new Date();
  var id = " " + date.getHours() + date.getMinutes() + date.getSeconds() + date.getMilliseconds();

  var listItem = document.createElement("li");
  listItem.id = "taakItem" + id;

  var checkBox = document.createElement("input");
  checkBox.type = "checkbox"; 
  checkBox.id = "checkbox" + id; 
  checkBox.addEventListener("click", updateItemStatus);

  var gebeurtenis = document.createElement("gebeurtenis");
  gebeurtenis.id = "item" + id; // item + tijd
  gebeurtenis.innerText = taak;
  gebeurtenis.addEventListener("dblclick", deleteItem); 

  listItem.appendChild(checkBox); 
  listItem.appendChild(gebeurtenis); 

  list.appendChild(listItem); 

}  

function updateItemStatus() {
  var checkboxId = this.id.replace("checkbox", "");
  var taak = document.getElementById("item" + checkboxId);

  if (this.checked) {
    taak.className = "checked"; // Geeft classnaam checked voor de     opmaak
  } else {
    taak.className = ""; // Als hij niet aangevinkt is, gebeurt er     niets
  }
}


function deleteItem() {
  var gebeurtenisId = this.id.replace("item", "");
  document.getElementById("taakItem" + gebeurtenisId).style.display =     "none";
}

var deleteAll = document.getElementById("deleteAll");

i prefer to do it with an array and a loop but i don't know how. 我更喜欢用数组和循环来做,但我不知道怎么做。 please help. 请帮忙。

 <script>
  function deleteAll(){
        document.getElementById("todoList").innerHTML = '';
    }
 </script>

 <button onclick="deleteAll()">Delete All</button>

Update 更新资料

<script>   
      function deleteAll(){
           var todo =  document.getElementById("todoList");
           var lis = todo.getElementsByTagName("li");
           console.log(lis);
           while(lis.length > 0){                   
              todo.removeChild(lis[0]);
           }
        };       

</script>

Without seeing your actual HTML it looks like when you add an event to your to-do list you're creating an <li> to a list. 在没有看到实际的HTML的情况下,将事件添加到待办事项列表时,您似乎正在为列表创建<li> To delete all of these, you could simply delete all the <li> s within the list. 要删除所有这些,您只需删除列表中的所有<li> If you're using JQuery it can be done with something like 如果您使用的是JQuery,则可以使用类似的方法

    $(list).empty();

In regular JavaScript (with a for loop) it will be something like (borrowing some of 在常规JavaScript(带有for循环)中,它将类似于(借用一些

    var ulList = getElementById(list);
    var childs = ul.children();        
    for (var i = 1; i < ulList.childNodes.length - 1; i++) {
        list.removeChild(list.childNodes[i]);   
    }

Instead of the original 代替原来的

   getElementById(list).innerHTML = '';

List would be the ID of the list you're passing into your function. List将是您要传递给函数的列表的ID。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM