gpt4 book ai didi

javascript - 添加待办事项列表项后如何将其删除

转载 作者:行者123 更新时间:2023-12-01 01:54:34 26 4
gpt4 key购买 nike

我真的尝试了一切,几乎 JS 中的每个功能。但添加后我无法从待办事项列表中删除该列表项。我尝试添加事件监听器、onclick=""等,但没有任何效果。有人可以看看我的代码并告诉我我做错了什么吗?谢谢您,非常感谢任何帮助。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>

<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>

<script src="script.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet href="style.css">
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<meta charset="UTF-8">
<title>To-Do-List</title>
</head>
<body>
<div class="container">
<div id="header">
<h1>My To Do List</h1>
</div>
<form autocomplete="off">
<input type="text" id="task" placeholder="Title...">
</form>
<button id="addTask">Add</button>
</div>

<br><br>

<div id="tasks">
<ul id="myUL">

</ul>
</div>
</div>
</div>
<script>

var list = document.getElementById('myUL');
list.addEventListener("click", boxChecked);
function boxChecked(event){
const element = event.target;
if(element.type === "close"){
delete element;
}
}

</script>
</body>
</html>

JS:

$(document).ready(function(){




document.getElementById('addTask').addEventListener("click", function(){
var task = document.getElementById('task').value;

if(task == ""){
alert("You Must Enter A Value!");
}else{
document.getElementById('myUL').innerHTML += '<li>' + task + '</li>' + '<button class="close" >&#10007</button>' + '<br><br>' + '<button class="check" >&#10003;</button>' + '<br><br>';
}

var button = document.querySelectorAll('.close');
for(var i = 0; i < button.length;i++){
button[i].addEventListener("click", function(){
this.parentNode.removeChild(this.previousSibling);
this.parentNode.removeChild(this);
});
}

var check = document.querySelectorAll('.check');
for(var i = 0; i < check.length;i++){
check[i].addEventListener("click", function(){
this.parentNode.style.textDecoration = "line-through";
});
}

});

});

最佳答案

当您将监听器附加到内部元素时,不要尝试连接 innerHTML 字符串,否则监听器将会丢失。相反,请使用 appendChild 正确附加元素。

delete element 实际上不会执行任何操作,因为 element 是一个变量,而不是对象的属性 - 尝试使用 .remove() 相反,删除 li (并将按钮放在 li 内,以便 HTML 有效)。

var list = document.getElementById('myUL');
list.addEventListener("click", boxChecked);

function boxChecked(event) {
const element = event.target;
if (element.className === "close") {
element.parentElement.remove();
}
}

document.getElementById('addTask').addEventListener("click", function() {
var task = document.getElementById('task').value;

if (task === "") return alert("You Must Enter A Value!");
const li = list.appendChild(document.createElement('li'));
li.textContent = task;
const button = li.appendChild(document.createElement('button'));
button.className = 'close';
button.textContent = '✗';
});
.close {
position: absolute;
left: 150px;
}
<div class="container">
<div id="header">
<h1>My To Do List</h1>
</div>
<form autocomplete="off">
<input type="text" id="task" placeholder="Title...">
</form>
<button id="addTask">Add</button>
</div>

<br><br>

<div id="tasks">
<ul id="myUL">

</ul>
</div>

关于javascript - 添加待办事项列表项后如何将其删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51145379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com