Blogger Information
Blog 14
fans 0
comment 0
visits 7541
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Todo list实践
Bruce.lau
Original
773 people have browsed it

示例地址

点击预览:TODO list

html部分:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>todolist</title>
  6. </head>
  7. <body>
  8. <script src="todo.js"></script>
  9. <label for="todo">
  10. <input type="text" name="todo" id="todo" placeholder="请输入事项" autofocus>
  11. </label>
  12. <button type="submit" onclick="add()">提交</button>
  13. <ul class = 'list'>
  14. </ul>
  15. </body>
  16. </html>

JS部分

  1. //找到节点获取内容容器
  2. let ul= document.querySelector('.list');
  3. function add () {
  4. let ul= document.querySelector('.list');//注释掉此行,21行提示报错为: Uncaught TypeError: Cannot read properties of null
  5. //获取表单元素的值
  6. let items = document.querySelector('#todo');
  7. // console.log(items.value);
  8. // console.log(items.value.length);
  9. //空值判断
  10. if (items.value.trim().length===0){
  11. alert('内容不能为空');
  12. items.focus()
  13. }
  14. //创建li元素
  15. let li = document.createElement('li');
  16. //将input的值赋给li元素
  17. li.innerHTML = items.value + `&nbsp;`+'<button onclick="del(this.parentNode)">删除</button>';
  18. // li.textContent ='item.value';
  19. //将li元素追加到ul下面
  20. ul.insertAdjacentElement('afterBegin', li);
  21. console.log(li);
  22. // ul.after(li);
  23. //清空输入框
  24. items.value=null;
  25. //焦点重置
  26. items.focus();
  27. }
  28. let del = function (items) {
  29. //删除确认
  30. confirm ('删除确认')?items.remove():false;
  31. //删除后自动获取焦点
  32. let it = document.querySelector('#todo');
  33. it.focus()
  34. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post