我用HTML、CSS和Javascript创建了一个简单的待办事项应用程序。此应用程序将列出待办事项。在这里,我们可以向待办事项应用程序添加新项目或从列表中删除该项目。我创建了一个函数,通过使用 appendchild(( 函数向应用程序添加新的待办事项。当我列出新的TODO时,我的列表中增加了一个空值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Todo application</title>
</head>
<body>
<div id="container">
<div class="controls" >
<h1>My TODO List</h1>
<p class="addtodo">Add new TODO items</p>
<input type="text" id="input">
<button type="button" id="add">Add TODO</button>
<button type="button" id="remove">Remove TODO</button>
</div>
<ul id="list">
<li class="mycheck"><input type="checkbox" id="check"><label>Attend Interviews</label></li>
<li class="mycheck"><input type="checkbox" id="check"><label>Visit Consultancy</label></li>
<li class="mycheck"><input type="checkbox" id="check" checked><label>Learn Aptitude</label></li>
</ul>
</div>
<script src="script.js"></script>
</body>
</html>
我需要向应用程序添加新的待办事项。待办事项应用程序必须准确地列出我需要的新项目。例如:去健身房[在我的代码中,我得到的是空而不是"去健身房"]。
var ul = document.getElementById('list');
var li;
var addButton = document.getElementById('add');
addButton.addEventListener('click', addItem);
var removeButton = document.getElementById('remove');
removeButton.addEventListener('click',removeItem)
//Function to add new TODO items
function addItem(){
var input = document.getElementById('input');
var item = input.nodeValue;
ul = document.getElementById('list');
var textnode = document.createTextNode(item);
if (item === ''){
return false;
}
else {
//create a "li" element
li = document.createElement('li');
//Create a checkbox
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.setAttribute('id','check');
var label = document.createElement('label');
label.setAttribute('for','item')
//adding elements to the webpage
ul.appendChild(label);
li.appendChild(checkbox);
label.appendChild(textnode);
li.appendChild(label);
ul.insertBefore(li, ul.childNodes[0]);
setTimeout(() => {
li.className = 'visual';
}, 3);
input.value = '';
}
}
var item = input.nodeValue;
它应该是var item = input.value;
var ul = document.getElementById('list');
var li;
var addButton = document.getElementById('add');
addButton.addEventListener('click', addItem);
var removeButton = document.getElementById('remove');
// removeButton.addEventListener('click',removeItem)
//Function to add new TODO items
function addItem() {
var input = document.getElementById('input');
var item = input.value;
ul = document.getElementById('list');
var textnode = document.createTextNode(item);
if (item === '') {
return false;
} else {
//create a "li" element
li = document.createElement('li');
//Create a checkbox
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.setAttribute('id', 'check');
var label = document.createElement('label');
label.setAttribute('for', 'item')
//adding elements to the webpage
ul.appendChild(label);
li.appendChild(checkbox);
label.appendChild(textnode);
li.appendChild(label);
ul.insertBefore(li, ul.childNodes[0]);
setTimeout(() => {
li.className = 'visual';
}, 3);
input.value = '';
}
}
<div id="container">
<div class="controls">
<h1>My TODO List</h1>
<p class="addtodo">Add new TODO items</p>
<input type="text" id="input">
<button type="button" id="add">Add TODO</button>
<button type="button" id="remove">Remove TODO</button>
</div>
<ul id="list">
<li class="mycheck"><input type="checkbox" id="check"><label>Attend Interviews</label></li>
<li class="mycheck"><input type="checkbox" id="check"><label>Visit Consultancy</label></li>
<li class="mycheck"><input type="checkbox" id="check" checked><label>Learn Aptitude</label></li>
</ul>
</div>