我有一个使用JavaScript的待办事项应用程序,可以根据其功能添加,删除条目。但是,一旦单击了“编辑”按钮,只有第一项可以响应,而其他项则无济于事。
如何使其响应并在此处进行编辑。
这是我的代码:
// Define UI Vars
const form = document.querySelector('#task-form');
const taskList = document.querySelector('.collection');
const clearBtn = document.querySelector('.clear-tasks');
const filter = document.querySelector('#filter');
const taskInput = document.querySelector('#task');
const editList = null;
// Load all event listeners
loadEventListeners();
// Load all event listeners
function loadEventListeners() {
// Add task event
form.addEventListener('submit', addTask);
// Remove task event
taskList.addEventListener('click', removeTask);
//edit task
//
// Clear task event
clearBtn.addEventListener('click', clearTasks);
// Filter tasks event
filter.addEventListener('keyup', filterTasks);
}
// Add Task
function addTask(e) {
if(taskInput.value === '') {
alert('Add a task');
}
// Create li element
const li = document.createElement('li');
// Add class
li.className = 'collection-item';
// Create text node and append to li
li.appendChild(document.createTextNode(taskInput.value));
// Create new link element
const link = document.createElement('a');
// Add class
link.className = 'delete-item secondary-content';
// Add icon html
link.innerHTML = '<i class="fa fa-remove"></i>';
// Append the link to li
li.appendChild(link);
// Append li to ul
taskList.appendChild(li);
const editButton=document.createElement("button");//edit button
editButton.className="editbtn";
editButton.innerText="Edit";//innerText encodes special characters, HTML does not.
li.appendChild(editButton);
taskList.appendChild(editButton);
const editList = document.querySelector('.editbtn');
editList.addEventListener('click',editTask)
// Clear input
taskInput.value = '';
e.preventDefault();
}
// Remove Task
function removeTask(e) {
if(e.target.parentElement.classList.contains('delete-item')) {
if(confirm('Are You Sure?')) {
e.target.parentElement.parentElement.remove();
}
}
}
// Clear Tasks
function clearTasks() {
// taskList.innerHTML = '';
// Faster
while(taskList.firstChild) {
taskList.removeChild(taskList.firstChild);
}
// https://jsperf.com/innerhtml-vs-removechild
}
// Filter Tasks
function filterTasks(e) {
const text = e.target.value.toLowerCase();
document.querySelectorAll('.collection-item').forEach(function(task){
const item = task.firstChild.textContent;
if(item.toLowerCase().indexOf(text) != -1){
task.style.display = 'block';
} else {
task.style.display = 'none';
}
});
}
function editTask (e) {
console.log('clicked')
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<title>Task List</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col s12">
<div id="main" class="card">
<div class="card-content">
<span class="card-title">Task List</span>
<div class="row">
<form id="task-form">
<div class="input-field col s12">
<input type="text" name="task" id="task">
<label for="task">New Task</label>
</div>
<input type="submit" value="Add Task" class="btn">
</form>
</div>
</div>
<div class="card-action">
<h5 id="task-title">Tasks</h5>
<div class="input-field col s12">
<input type="text" name="filter" id="filter">
<label for="filter">Filter Tasks</label>
</div>
<ul class="collection"></ul>
<a href="#" class="clear-tasks btn black">Clear Tasks</a>
</div>
</div>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="app.js"></script>
</body>
</html>
感谢您阅读我的问题
致谢
答案 0 :(得分:3)
因为const editList = document.querySelector('.editbtn');
正在选择找到的第一个按钮并添加点击。它不是选择您刚刚添加的按钮。因此,第一个元素不是多次触发该功能,因为您不断向它添加越来越多的事件侦听器。
您已经具有对该按钮的引用,因此无需查找它。
editButton.addEventListener('click', editTask)
答案 1 :(得分:0)
document.querySelector()
返回第一个匹配的元素。每次您调用此querySelector()
时,它将返回第一个编辑按钮。直接addEventListener
到editButton
,而不是从querySelector()
editButton.addEventListener('click',editTask)