我一直在创建一个待办事项列表,每个条目有两个按钮,“完成”用于逐行完成的任务,“删除”用于删除它。当我删除第二个项目时,它会删除第一个项目而不是第二个项目。我怎样才能解决这个问题?谢谢。
这是 HTML
<body>
<header>
<h1>To Do List</h1>
<div class="addItems">
<input type="text" name="text" id="addThis" onfocus="this.value=''">
<input type="button" value="Add" id="addBtn">
</div>
</header>
<section class="checkList">
<ul class="list"></ul>
</section>
这是JS
addButton.addEventListener('click' , add);
function add(){
let input = document.getElementById('addThis').value;
let newLi = document.createElement('li');
newLi.id = "listNewItem";
newLi.style.listStyleType = "none";
newLi.style.backgroundColor = "#f4f4f4";
newLi.style.padding = "10px";
newLi.style.marginBottom = "10px";
newLi.appendChild(document.createTextNode(input));
let deleteBtn = document.createElement('button')
console.log(newLi);
let list = document.querySelector('.list');
list.insertBefore(newLi, list.childNodes[2]);
let delBtn = document.createElement('button');
delBtn.id = "doneButn";
delBtn.className = "delete";
delBtn.setAttribute("onclick" , "done()");
delBtn.appendChild(document.createTextNode("Remove"));
newLi.appendChild(delBtn);
let doneSubBtn = document.createElement('button');
doneSubBtn.id = "alreadyDone";
doneSubBtn.className = "done";
doneSubBtn.setAttribute("onclick" , "finished()");
doneSubBtn.appendChild(document.createTextNode("Done"));
newLi.appendChild(doneSubBtn);
}
function done(){
let del = document.getElementById('doneButn');
let li = del.parentNode;
li.remove();
}
function finished(){
let liText = document.getElementById('alreadyDone').parentNode;
liText.style.textDecoration = "line-through";
}
javascripthtmlCSS
MMTTMM
相关分类