2 回答
![?](http://img1.sycdn.imooc.com/5859e2d50001f6bb01000100-100-100.jpg)
TA贡献1818条经验 获得超7个赞
// The array
const todos = [{
text: 'wake up',
completed: true
}, {
text: 'get some food',
completed: true
}, {
text: 'play csgo',
completed: false
}, {
text: 'play minecraft',
completed: true
}, {
text: 'learn javascript',
completed: false
}];
//creating p elements and assigning their text content to each "text" property of the "todos" array
todos.forEach(function(todo) {
let p = document.createElement('p');
p.textContent = todo.text;
document.querySelector('#todo').appendChild(p);
})
document.querySelector('button').addEventListener('click', function(e) {
e.preventDefault();
var todo = document.querySelector('input').value;
todos.push({
text: todo,
completed: false
}); document.querySelector('#todo').insertAdjacentHTML('beforeend', '<p>' + todo + '</p>');
});
<h1>Todos</h1>
<div id="todo"></div>
<form id="form">
Add a new todo
<input type="text" placeholder="Type your first name" name="firstName">
<button>Submit</button>
</form>
添加回答
举报