【问题标题】:How do I append more than one child element to a parent element Javascript如何将多个子元素附加到父元素Javascript
【发布时间】:2019-10-16 14:00:31
【问题描述】:

大家好,我是编程新手,如果我的问题看起来很愚蠢或太简单,我很抱歉。我正在尝试在父元素上附加 4 个不同的子元素。

我正在构建一个待办事项列表应用程序,我希望该应用程序以这样一种方式工作,即当输入并保存“任务”时,输入的任务将出现在带有复选框、删除按钮的列表中,和一个编辑按钮。我尝试使用 .appendchild() 将子元素附加到其父元素上,但它不起作用。

<Ol id="ol">
    <li>
        <input type="checkbox">Read a novel
        <button>Edit</button>
        <button>Delete</button>
    </li>
    <li>
        <input type="checkbox">Take a walk
        <button>Edit</button>
        <button>Delete</button>
    </li>
</Ol>






let inputToDoList= document.getElementById('inputToDoList');

let addButton=  document.getElementById('addButton');
let editButton= document.createElement('button');
let deleteButton= document.createElement('button');
let checkInput= document.createElement('input');

checkInput.type= "checkbox"
deleteButton.innerText= "Delete"
editButton.innerText= "Edit"


addButton.addEventListener('click', () => {
   let ol= document.getElementsByTagName('ol')[0];
   let li= document.createElement('li');

   li.textContent= inputToDoList.value
   ol.appendChild(checkInput) 
   ol.appendChild(li)
   ol.appendChild(editButton)
   ol.appendChild(deleteButton)

   if(inputToDoList.value.length > 0){
      inputToDoList.value='';
}
});

我希望程序做的是在每次单击“保存”按钮时添加 inputToDoList.value、复选框、编辑按钮和删除按钮,但该功能仅在我第一次单击保存按钮时才有效时间。当我随后单击“保存”按钮时,只有 inputToDoList.value 添加到列表中。将不再添加其他元素,即复选框、编辑按钮和删除按钮。

【问题讨论】:

    标签: javascript html dom


    【解决方案1】:

    你必须使用 cloneNode => https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode

    // making of LI type element, with checkBox, Text and buttons Edit / delete :
    const LI_toDo      = document.createElement('li')
    ,     chk_toDo     = document.createElement('input')
    ,     txt_toDo     = document.createElement('span')
    ,     bt_edit_toDo = document.createElement('button')
    ,     bt_del_toDo  = document.createElement('button')
    ;
    chk_toDo.type            = "checkbox";
    bt_edit_toDo.textContent = 'Edit';
    bt_del_toDo.textContent  = 'Delete';
    
    LI_toDo.appendChild(chk_toDo);
    LI_toDo.appendChild(txt_toDo);
    LI_toDo.appendChild(bt_edit_toDo);
    LI_toDo.appendChild(bt_del_toDo);
    // - - - - - - - - - -- - - - -- - - -making completed...
    
    const inputToDoList = document.getElementById('inputToDoList')
    ,     List_ol       = document.getElementById('ol')
    ;
    
    document.getElementById('addButton').onclick = function()
    {
      txt_toDo.textContent  = inputToDoList.value;
      inputToDoList.value   = '';
      List_ol.appendChild( LI_toDo.cloneNode(true) );
    }
    
    
    List_ol.onclick = function(e)              //  clicks events over 'ol' elements
    {                     
      if (!e.target.matches('button')) return; // check if it is on a button
    
      switch (e.target.textContent)
      {
        case 'Edit':    EditButton(e.target.parentNode);    break; //  e.target.parentNode 
        case 'Delete':  DeleteButton(e.target.parentNode);  break; // === LI parent of clicked button 
      }
    }
    
    function EditButton(toDo_LI)
    {
      let SpanTxt = toDo_LI.querySelector('span')
      ,   change  = prompt('Edit..',SpanTxt.textContent )
      ;
      if (change) { SpanTxt.textContent = change }
    }
    
    function DeleteButton(toDo_LI)
    {
      let SpanTxt = toDo_LI.querySelector('span')
      ,   Del     = confirm(`please confirm delete of ${SpanTxt.textContent}`)
      ;
      if (Del) { List_ol.removeChild( toDo_LI ) }
    }
    span {
      display: inline-block;
      width: 200px;
      padding : 0 5px 5px 0; 
      border-bottom :1px solid lightblue;
    }
    button { margin: 5px }
    <div>
      <label>what to do : </label><input id="inputToDoList" type="text">
      <button id="addButton">add</button>
    </div>
    
    <ol id="ol"></ol>

    【讨论】:

    • 你知道如何处理所有的编辑/删除按钮吗?
    • 我一直在尝试使编辑和删除按钮起作用,但它们不起作用。对于删除按钮,我尝试使用this.parentnode 获取父元素并使用``listItem.parentNode`` 和.removechild 获取子元素。但我不明白。这是删除按钮的代码``` deleteButton.addEventListener('click', ()=>{ let listItem= this.parentNode; let ul= listItem.parentNode; ul.removeChild(listItem); });`` `
    • 不,您必须使用事件委托。我将更改我的代码以显示如何
    • 我做了(之前准备的)请测试一下
    • 谢谢,这两个按钮现在都运行良好。如果你不介意我问,代码对我来说看起来很复杂。是否有我可以阅读的材料或文档以便更好地理解。
    【解决方案2】:

    在点击事件中创建按钮元素。当前,您将 createElement 放置在 addButton 事件之外。根据您的要求,应在添加点击事件中创建按钮,因此元素创建应在添加点击事件中进行。

    let inputToDoList = document.getElementById('inputToDoList');
    
    let addButton = document.getElementById('addButton');
    
    let ol = document.getElementById('ol');
    addButton.addEventListener('click', () => {
      let editButton = document.createElement('button');
      let deleteButton = document.createElement('button');
      let checkInput = document.createElement('input');
      checkInput.type = "checkbox"
      deleteButton.innerText = "Delete"
      editButton.innerText = "Edit"
      let li = document.createElement('li');
      li.textContent = inputToDoList.value
      ol.appendChild(checkInput)
      ol.appendChild(li)
      ol.appendChild(editButton)
      ol.appendChild(deleteButton)
    
      if (inputToDoList.value.length > 0) {
        inputToDoList.value = '';
      }
    });
    <input type="text" id="inputToDoList" />
    <button id="addButton">
    Add
    </button>
    <ol id="ol">
    
    </ol>

    【讨论】:

    • @Missawi:很高兴它帮助您了解代码中发生的事情。
    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2018-05-19
    • 2016-08-22
    • 2021-10-26
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多