要在待办事项列表中显示删除按钮,可以使用HTML、CSS和JavaScript来实现。以下是一个简单的示例,展示了如何创建一个待办事项列表,并在每个待办事项旁边添加一个删除按钮。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>待办事项列表</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="todo-app">
<h1>待办事项</h1>
<input type="text" id="todo-input" placeholder="添加新的待办事项">
<button id="add-todo">添加</button>
<ul id="todo-list"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
}
#todo-app {
width: 300px;
margin: 0 auto;
}
#todo-input {
width: 200px;
padding: 10px;
margin-right: 10px;
}
#todo-list {
list-style-type: none;
padding: 0;
}
.todo-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #ccc;
}
.delete-btn {
background-color: #ff4d4d;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
}
document.getElementById('add-todo').addEventListener('click', function() {
const input = document.getElementById('todo-input');
const newItem = document.createElement('li');
newItem.className = 'todo-item';
newItem.textContent = input.value;
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = '删除';
deleteBtn.addEventListener('click', function() {
newItem.remove();
});
newItem.appendChild(deleteBtn);
document.getElementById('todo-list').appendChild(newItem);
input.value = '';
});
这种待办事项列表和删除按钮的组合非常适用于任务管理应用、个人日程安排、项目管理工具等场景。
如果在实现过程中遇到问题,例如删除按钮不起作用,可以检查以下几点:
通过这种方式,你可以轻松地在待办事项列表中显示并使用删除按钮。
领取专属 10元无门槛券
手把手带您无忧上云