1 回答
TA贡献1848条经验 获得超10个赞
两个问题:首先,正如 Matt 指出的那样,您不应该使用Arrayas 变量名——它是一个保留字——所以Array改为arr. 其次,删除按钮需要针对input同一li元素中的标记。因此,更改变量名称并定位正确的input标签,我得到:
var arr = [];
function myFunction() {
//console.log('test');
var todoItem = document.getElementById('new-todo-item').value;
const Input = document.querySelectorAll('input[name="todo-item-done"]');
//console.log(Input.value);
//Input von Add
arr.unshift(todoItem);
document.getElementById("todo-list").innerHTML +=
"<li>" +
"<input type='text'" + " name='todo-item-done'" +
" class='todo-item-done'" + " value='" + todoItem +
"'> " +
"<a id= 'myDIV' class='up' onclick='UpDown()' href='#'>" + "up"+"</a>" +
" | " +
"<a class='down' onclick='UpDown()' href='#'>" + "down"+"</a> " +
"<button onclick='deleteData(this)' class='todo-item-delete'>Delete</button>" +
"</li>"
;
document.getElementById('new-todo-item').value = "";
}
function deleteData(e) {
console.log(arr);
let p = e.parentNode;
console.log(p);
let deleteValue = p.getElementsByTagName("input")[0].value;
var index = arr.indexOf(deleteValue);
if (index !== -1) {
arr.splice(index, 1);
}
p.remove();
console.log(arr);
}
<h1>To-Do List</h1>
<label for="todo">To do:</label>
<input type="text" id="new-todo-item">
<input onclick="myFunction()" type="submit" id="add-todo-item" value="add the todo">
<ul id="todo-list" class="todo-list"></ul>
添加回答
举报