Blogger Information
Blog 37
fans 0
comment 0
visits 14262
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类数组和纯数组区别,DOM的操作
秋闲独醉
Original
296 people have browsed it
  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. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. list-style: none;
  14. text-decoration: none;
  15. font-size: 1.5rem;
  16. font-weight: bold;
  17. }
  18. html {
  19. font-size: 16px;
  20. }
  21. fieldset {
  22. width: 400px;
  23. height: 200px;
  24. /* position: absolute; */
  25. margin: 1rem auto;
  26. box-shadow: 0.1rem 0.2rem 0.6rem #888;
  27. border-radius: 1.8rem 0rem 1.8rem 0;
  28. background: linear-gradient(to bottom, lightgreen, white);
  29. }
  30. fieldset legend {
  31. border-radius: 1.8rem 0rem 1.8rem 0;
  32. text-align: center;
  33. border: 1px solid gray;
  34. padding: 0.8rem;
  35. background: linear-gradient(to top, red, white);
  36. color: white;
  37. font-style: italic;
  38. }
  39. .item {
  40. margin-left: 1rem;
  41. margin-top: 1rem;
  42. }
  43. .item input {
  44. margin-left: 2rem;
  45. width: 250px;
  46. border: none;
  47. background: none;
  48. border-bottom: 1px solid gray;
  49. }
  50. button {
  51. margin-top: 15px;
  52. margin-left: 150px;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <!-- 1. 自定义类数组(伪数组),并说出与纯数组的区别与联系,哪些地方会用到类数组
  58. 2. 获取dom元素的API有几个,他们的使用场景是什么?
  59. 3. 如何优雅的获取form表单元素与控件的值?
  60. 4. dom元素的遍历与常用API有哪些, 实例演示 -->
  61. <script>
  62. //纯数组/真数组
  63. //1.每个值都对应一个键名,键名是从0开始递增且是正整数,如0,1,2,3...
  64. //2.有个length属性,表示数组长度
  65. //3.prototype是Array
  66. const colors = ["red", "orange", "blue", "green"];
  67. console.log(colors);
  68. // console.log(colors instanceof Array); //true
  69. // console.log(colors instanceof Object); //true
  70. // console.log(colors.pop());
  71. //类数组/伪数组
  72. const animals = {
  73. 0: "dog",
  74. 1: "cat",
  75. 2: "pig",
  76. length: 3,
  77. };
  78. console.log(animals);
  79. // console.log(animals.pop());
  80. //类数组不具备数组的原型方法,不可以调用相关数组方法
  81. </script>
  82. <ul class="list">
  83. <li class="item">item1</li>
  84. <li class="item">item2</li>
  85. <li class="item">item3</li>
  86. <li class="item">item4</li>
  87. <li class="item">item5</li>
  88. <li class="item">item6</li>
  89. </ul>
  90. <script>
  91. //dom元素的API有querySelector()和 querySelectorAll()
  92. //querySelector()获取的是单个元素
  93. const item = document.querySelector("ul li:nth-of-type(3)");
  94. console.log(item); //原型是HTMLElement
  95. item.style.color = "red";
  96. //querySelectorAll()获取的是一组无素,返回类数组
  97. const items = document.querySelectorAll("ul .item:nth-last-of-type(-n +2)");
  98. console.log(items);
  99. //forEach(回调函数)遍历
  100. //function(当前正在遍历的值,当前值对应的索引)
  101. // items.forEach(function (v, k) {
  102. // v.style.color = "blue";
  103. // });
  104. //简写
  105. items.forEach((v, k) => (v.style.color = "blue"));
  106. //for of 遍历
  107. for (let v of items.values()) {
  108. console.log(v);
  109. }
  110. </script>
  111. <form action="" method="dialog">
  112. <fieldset>
  113. <legend>用户登录</legend>
  114. <div class="item">
  115. <div class="item1">
  116. <label for="email">邮箱:</label>
  117. <input type="email" name="email" id="email" value="jdd@22.cm" placeholder="填写邮箱" />
  118. </div>
  119. <div class="item2">
  120. <label for="password">密码:</label>
  121. <input type="password" name="password" id="password" value="222" placeholder="输入密码" />
  122. </div>
  123. </div>
  124. <button>提交</button>
  125. </fieldset>
  126. </form>
  127. <script>
  128. //获取form表单元素与控件值
  129. //获取当前页的所有表单数据
  130. //forms[0],获取第一个表单数据
  131. const value = document.forms[0];
  132. console.log(value);
  133. const form = document.querySelector("form");
  134. console.log(form);
  135. //获取email元素
  136. console.log(document.forms[0].email);
  137. //获取email元素的值
  138. let email = document.forms[0].email.value;
  139. //获取password元素
  140. console.log(document.forms[0].password);
  141. //获取password元素的值
  142. let password = document.forms[0].password.value;
  143. let userInfo = { email, password };
  144. console.log(JSON.stringify(userInfo));
  145. </script>
  146. <script>
  147. //4. dom元素的遍历与常用API有哪些, 实例演示
  148. // 节点类型
  149. // windows:全局对象
  150. // document:文档对象,就是当前的html文件
  151. // element:元素对象.
  152. // text:文件对象
  153. let ul = document.querySelector(".list");
  154. console.log(ul);
  155. //获取ul下的所有节点
  156. console.log(ul.childNodes);
  157. //获取ul下的标签节点
  158. ul.childNodes.forEach((node) => {
  159. if (node.nodeType == 1) {
  160. console.log(node);
  161. }
  162. });
  163. //只返回元素类型节点
  164. console.log(ul.children);
  165. //将类数组转为纯数组1
  166. console.log(Array.from(ul.children));
  167. //将类数组转为纯数组2
  168. console.log([...ul.children]);
  169. //ul下的第一个节点
  170. ul.firstElementChild.style.backgroundColor = "red";
  171. //ul下的第一个节点,的下一个节点
  172. ul.firstElementChild.nextElementSibling.style.backgroundColor = "orange";
  173. //ul下的最后一个节点,的前一个节点
  174. ul.lastElementChild.previousElementSibling.style.backgroundColor = "purple";
  175. //ul下的最后一个节点
  176. ul.lastElementChild.style.backgroundColor = "pink";
  177. </script>
  178. </body>
  179. </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