亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 函數clearList 的問題

Javascript 函數clearList 的問題

Go
泛舟湖上清波郎朗 2023-08-21 15:00:43
我創建了一個包含 HTML、CSS 和 Javascript 的列表,您可以在下面看到。它工作正常,直到我按“清除”并且列表被清除,并且清除按鈕也消失了。我仍在學習 Javascript,不確定我的代碼中有什么問題。有人可以幫忙我需要添加/刪除哪一行代碼來阻止此錯誤的發生嗎?(() => {  const listElement = document.getElementById('wishlist');  const newItem = document.getElementById('newItem');  const addBtn = document.getElementById('addBtn');  const clearBtn = document.getElementById('clearBtn');  //add new destination to the list  function addItem(item) {    const itemElement = document.createElement('li');    itemElement.textContent = item;    const deleteButton = document.createElement('button');    deleteButton.textContent = 'x';    itemElement.appendChild(deleteButton);    deleteButton.addEventListener('click', ev => { // <- new      listElement.removeChild(itemElement); // <- new    }); // <- new    listElement.appendChild(itemElement);  };  function clearList() {    while (listElement.firstChild) {      listElement.removeChild(listElement.firstChild);    }  }  function renderList(list) {    list.forEach(addItem);  }  addBtn.addEventListener('click', ev => {    newItem.value.split(',').forEach(v => {      if (v) {        addItem(v);      }    });    newItem.value = null;  });  clearBtn.addEventListener('click', ev => {    clearList();  });  window.addEventListener('beforeunload', ev => {    const items = [...listElement.childNodes];    if (items.length) {      const list = items.map(item => {        return item.textContent.slice(0, -1);      });      localStorage.setItem('destination-list', list);    } else {      localStorage.removeItem('destination-list');    }  });  window.addEventListener('DOMContentLoaded', ev => {    const shoppingList = localStorage.getItem('destination-list');    if (destinationList) {      renderList(destinationList.split(','));    }  });  newItem.addEventListener("keyup", ev => {    if (ev.key === "Enter") {      addBtn.click();    }  });})()
查看完整描述

2 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

問題是因為您將清除按鈕保留在 ol 內,并在按下清除時刪除列表的子項,將其移到 ol 之外,它將像下面這樣工作:


(() => {


  const listElement = document.getElementById('wishlist');

  const newItem = document.getElementById('newItem');

  const addBtn = document.getElementById('addBtn');

  const clearBtn = document.getElementById('clearBtn');


//add new destination to the list

  function addItem(item) {

    const itemElement = document.createElement('li');

    itemElement.textContent = item;

    const deleteButton = document.createElement('button');

    deleteButton.textContent = 'x';

    itemElement.appendChild(deleteButton);

    deleteButton.addEventListener('click', ev => { // <- new

      listElement.removeChild(itemElement); // <- new

    }); // <- new

    listElement.appendChild(itemElement);

  };


  function clearList() {

    while (listElement.firstChild) {

      listElement.removeChild(listElement.firstChild);

    }

  }


  function renderList(list) {

    list.forEach(addItem);

  }


  addBtn.addEventListener('click', ev => {

    newItem.value.split(',').forEach(v => {

      if (v) {

        addItem(v);

      }

    });

    newItem.value = null;

  });


  clearBtn.addEventListener('click', ev => {

    clearList();

  });


  window.addEventListener('beforeunload', ev => {

    const items = [...listElement.childNodes];

    if (items.length) {

      const list = items.map(item => {

        return item.textContent.slice(0, -1);

      });

      localStorage.setItem('destination-list', list);

    } else {

      localStorage.removeItem('destination-list');

    }

  });


  window.addEventListener('DOMContentLoaded', ev => {

    const shoppingList = localStorage.getItem('destination-list');

    if (destinationList) {

      renderList(destinationList.split(','));

    }

  });


  newItem.addEventListener("keyup", ev => {

    if (ev.key === "Enter") {

      addBtn.click();

    }

  });


})()

ol{

  padding: 1em 0;

  margin: 0;

}


li button {

  opacity: 0.05;

  transition: 0.4s;

  background: none;

  border: none;

}

li:hover button {

  opacity: 0.4;

}

li button:hover {

  opacity: 1;

}

button, input {

  font-size: inherit;

}

input {

  padding-left: 1em;

  flex: 1;

  min-width: 5em;

}

#clearBtn{

  width: 100%;

}


li button {

  opacity: 0.05;

  transition: 0.4s;

  background: none;

  border: none;

}

li:hover button {

  opacity: 0.4;

}

li button:hover {

  opacity: 1;

}

button, input {

  font-size: inherit;

}

input {

  padding-left: 1em;

  flex: 1;

  min-width: 5em;

}

<div class="destination-list">

<h2>Destination Wish List</h2>

<input placeholder="New Destination" id="newItem">

<button id="addBtn">Add</button>

<ol id="wishlist">

</ol>

<button id="clearBtn">Clear</button>


查看完整回答
反對 回復 2023-08-21
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

<div class="destination-list">

  <h2>Destination Wish List</h2>

  <input placeholder="New Destination" id="newItem">

  <button id="addBtn">Add</button>

  <button id="clearBtn">Clear</button>

  <ol id="wishlist">


  </ol>


查看完整回答
反對 回復 2023-08-21
  • 2 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號