Blogger Information
Blog 94
fans 0
comment 0
visits 92495
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】JS获取表单元素和遍历Dom树***前后端交互*重点掌握
可乐随笔
Original
502 people have browsed it

表单元素获取和遍历Dom树**前后端交互重点掌握

1.表单元素获取

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>获取表单元素</title>
  8. </head>
  9. <body>
  10. <form action="login.php" method="post" id="login">
  11. <fieldset class="login">
  12. <legend>用户名称</legend>
  13. <label for="email">邮箱:</label>
  14. <input type="email" name="email" id="email" value="admin@qq.com" />
  15. <label for="password">密码:</label>
  16. <input
  17. type="password"
  18. name="password"
  19. id="password"
  20. value="123456"
  21. autocomplete
  22. />
  23. <button>提交</button>
  24. </fieldset>
  25. </form>
  26. <script>
  27. // 1. 获取表单:forms.id
  28. console.log(document.forms.login);
  29. // 2. 获取表单控件:forms.id.name //id和name都可以获取控件元素
  30. console.log(document.forms.login.email);
  31. // 3. 获取表单控件的值,forms.id.name.value
  32. console.log(document.forms.login.email.value);
  33. // 前后端分离
  34. // 前端 <-> (JSON格式的字符串) <-> 服务器
  35. // 将表单中邮箱和密码发送到服务器端
  36. // 第一步:获取表单邮箱和密码
  37. let login = document.forms.login;
  38. let email = login.email.value;
  39. let password = login.password.value;
  40. console.log(email, password);
  41. // 第二步:转为JS对象
  42. let user = { email, password };
  43. // 第三步:把JS对象序列化为JSON字符串
  44. let json = JSON.stringify(user)
  45. console.log(json)
  46. /**
  47. * * 总结
  48. * * 1. 表单:form.id
  49. * * 2. 表单控件:input.name
  50. * * 3. 表单控件的值:input.name.value
  51. */
  52. </script>
  53. </body>
  54. </html>

2.遍历Dom树

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>遍历Dom树</title>
  8. </head>
  9. <body>
  10. <ul class="list">
  11. <li>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. <li>item6</li>
  17. </ul>
  18. <script>
  19. /**
  20. * * 节点类型(12个,常用3个)
  21. * * 1. document:文档类型,9
  22. * * 2. element:元素类型,1
  23. * * 3. text:文本类型,3
  24. */
  25. // 列表 <ul class='list'>
  26. let list = document.querySelector('.list');
  27. // nodeType 节点类型
  28. console.log(list.nodeType);
  29. // 1. 获取所有子节点:childNodes
  30. let items = list.childNodes;
  31. console.log(items);
  32. //输出了13个子节点,包括7个文本节点(控制元素:回车符、空格符等)
  33. //输出6个元素结点
  34. //返回的是一个类数组,可以用forEach属性遍历
  35. // 2.将类数组转换为真数组
  36. // 2.1 Array.from
  37. console.log(Array.from(items));
  38. // 2.2. ...rest
  39. console.log([...items]);
  40. // 转为数组之后,Array很多API都可以调用
  41. // 3. 将数组中的元素类型节点返回
  42. let result = [...items].filter((item) => item.nodeType === 1);
  43. console.log(result);
  44. //3.1 封装一个函数,方便获取子元素使用
  45. const getChildren = (items) =>
  46. [...items].filter((item) => item.nodeType === 1);
  47. //使用这个函数API
  48. console.log(getChildren(items));
  49. //3.2 children: js已经给我们封装好了一个属性,children
  50. console.log(list.children);
  51. //children 获取的是类数组
  52. //3.3 取第一个子元素
  53. //ele.textContent:元素内的文本
  54. console.log(list.children[0].textContent);
  55. // 4. 语法糖
  56. // 4.1 拿到第一个元素
  57. let first = list.firstElementChild;
  58. console.log(first);
  59. // 4.2 拿到下一个兄弟
  60. let next = list.querySelector(':first-child + *');
  61. next.style.color = 'blue';
  62. next = first.nextElementSibling;
  63. //4.3 最后一个
  64. let last = list.lastElementChild;
  65. last.style.color = 'red';
  66. //4.4 前一个兄弟
  67. let prev = last.previousElementSibling;
  68. prev.style.color = 'violet';
  69. //4.5 父节点
  70. let parnet = first.parentElement;
  71. parnet.style.border = '1px solid';
  72. // 5. 遍历
  73. // {}[]()前面加;
  74. [...list.children].forEach((item) => (item.style.border ='1px solid'));
  75. </script>
  76. </body>
  77. </html>
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