我正在尝试添加另一个按钮,该按钮将从列表中删除任务,并允许用户删除其中任何一个。我正在尝试将 splice 与 indexOf 一起使用,但到目前为止它还没有工作。这是代码。谢谢您的帮助。
// tasks.js #2
// This script manages a to-do list.
// Need a global variable:
var tasks = [];
// Function called when the form is submitted.
// Function adds a task to the global array.
function addTask() {
'use strict';
// Get the task:
var task = document.getElementById('task');
// Reference to where the output goes:
var output = document.getElementById('output');
// For the output:
var message = '';
if (task.value) {
// Add the item to the array:
tasks.push(task.value);
// Update the page:
message = '<h2>To-Do</h2><ol>';
for (var i = 0, count = tasks.length; i < count; i++) {
message += '<li>' + tasks[i] + '</li>';
}
message += '</ol>';
output.innerHTML = message;
} // End of task.value IF.
// Return false to prevent submission:
return false;
} // End of addTask() function.
function deleteTask() {
var inputTask = document.getElementById('task');
var taskLength = inputTask.length;
var i = array.indexOf("inputTask");
if (i != -1) {
array.splice(i, taskLength);
}
}
// Initial setup:
function init() {
'use strict';
//document.getElementById('theForm').onsubmit = addTask;
var elem1 = document.getElementById("submit");
elem1.addEventListener("click", addTask, false);
var elem2 = document.getElementById("delete");
elem2.addEventListener("click", deleteTask, false);
} // End of init() function.
window.onload = init;