[英]Why does my delete button not work unless I use a different delete button in a different div?
I am learning JavaScript and trying to create a todo app and when I hover my active lists, I want to be able to delete the content of that list while it's active.我正在学习 JavaScript 并尝试创建一个待办事项应用程序,当我 hover 我的活动列表时,我希望能够在它处于活动状态时删除该列表的内容。 Instead, it deletes the content when I click on a div with dummy data on it that I use just for reference that also has a delete button.相反,当我单击一个带有虚拟数据的 div 时,它会删除内容,我仅用作参考,它也有一个删除按钮。 These are the snippets that I need help with.这些是我需要帮助的片段。
I am not sure what else to write down.我不知道还要写什么。 Apparently I need more words just to ask this specific question so I am just randomly writing just so I can push this out and ask for help.显然我需要更多的词来问这个特定的问题,所以我只是随机写,这样我就可以把它推出并寻求帮助。
https://jsfiddle.net/delo2795/xbp8vfeh/5/ https://jsfiddle.net/delo2795/xbp8vfeh/5/
const listsContainer = document.querySelector('[data-lists]')
listsContainer.addEventListener('mouseover', e => {
if (e.target.tagName.toLowerCase() === 'li') {
selectedListId = e.target.dataset.listID
console.log(selectedListId)
saveAndRender()
}
})
deleteListButton.addEventListener('click', e => {
// find lists of tasks that is not selected
lists = lists.filter(list => list.id !== selectedListId)
// take the list that is selected and remove from memory
selectedListId = null
saveAndRender()
})
newListForm.addEventListener('submit', e => {
e.preventDefault()
// Get information from the user form
const taskInput = taskname.value
const assignInput = assign.value
const descriptionInput = description.value
const dueDateInput = duedate.value
// user has to input something in the field
// if (taskInput == null || taskInput === '') return
// if (assignInput == null || assignInput === '') return
// if (descriptionInput== null || descriptionInput === '') return
// if (dueDateInput == null || dueDateInput === '') return
// create List
const list = createList(taskInput, assignInput, descriptionInput,dueDateInput)
// Update the List
lists.push(list)
// save List to Browser and Show List on Browser
saveAndRender()
})
function createList(taskInput, taskAssign, taskDescription, taskDueDate) {
// Initializes the variables and saves it to list
return {
id: Date.now().toString(),
taskInput: taskInput,
taskAssign: taskAssign,
taskDescription: taskDescription,
taskDueDate: taskDueDate,
taskCompleted: false
}
}
function render() {
clearElement(listsContainer)
lists.forEach(list => {
// create new list item container
const listElement = document.createElement('li')
listElement.dataset.listID = list.id
listElement.classList.add('list-group-item', 'shadow-lg', 'rounded')
if (list.id === selectedListId) {
listElement.classList.add('activeList')
}
}
}
render()
You should use an array to store the data.您应该使用数组来存储数据。 When a user clicks on a delete button (which will be assigned a function like DELETE(id)), the JS will delete it from the array and then rerender the notes from the array.当用户点击删除按钮(将被分配一个 function 像 DELETE(id))时,JS 将从数组中删除它,然后从数组中重新呈现注释。 This will help keep your app more structured.这将有助于使您的应用程序更加结构化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.