【问题标题】:Delete all button in to do app [duplicate]删除应用程序中的所有按钮[重复]
【发布时间】:2018-01-30 11:30:00
【问题描述】:

我尝试将全部删除添加到我的待办事项应用程序中。 我有这样的事情:

function removeAll(){
        var ol = document.getElementsByTagName('ol');
        if(ol.lenght > 0){
            ol.remove();
        }
    }
document.getElementById('delete-all').addEventListener('click', removeAll);

<input type="text" id="text-field">
<input type="button" id="add-task" value="dodaj zadanie!">
<input type="button" id="delete-all" value="usuń wszystko">

<div id="to-do-list-container">
    <ul id="task-list">
        <ol>damian</ol>
    </ul>
</div>

它显示没有错误...我检查是否存在带有标签 ol 的元素,然后尝试删除所有带有 ol 标签的元素。我试过 ol.parrentNode.remove();同样的效果...

【问题讨论】:

  • lenght ??是错字吗?
  • 除了错字之外,&lt;ol&gt; 不是&lt;ul&gt; 的有效子代
  • 哦...但现在它显示:未捕获的类型错误:ol.remove is not a function at HTMLInputElement.removeAll
  • 我为 li 改了(之前忘记改了...)——不是 vanilla js?
  • ol 是一个节点列表,尝试“删除”那个是没有意义的。您需要遍历该节点列表中包含的元素,然后单独删除它们。 (在这样做的同时,您需要记住节点列表是“实时的”。)

标签: javascript removechild


【解决方案1】:

使用while 循环尝试以下操作:

function removeAll(){
  var list = document.getElementById("task-list");
  while(list.firstChild){
    list.removeChild(list.firstChild);
  }
}
document.getElementById('delete-all').addEventListener('click', removeAll);
<input type="text" id="text-field"/>
<input type="button" id="add-task" value="dodaj zadanie!"/>
<input type="button" id="delete-all" value="usuń wszystko"/>

<div id="to-do-list-container">
    <ul id="task-list">
        <ol>damian</ol>
        <ol>damian2</ol>
        <ol>damian3</ol>
    </ul>
</div>

【讨论】:

    猜你喜欢
    • 2015-08-22
    • 2016-11-11
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多