Blogger Information
Blog 35
fans 0
comment 0
visits 17009
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类数组与DOM元素API即DOM元素的遍历
三九三伏
Original
298 people have browsed it

一、类数组

纯数组

  1. ...
  2. <script>
  3. const arr = ['red', 'green', 'blue'];
  4. console.log(arr);
  5. // 数组也是对象
  6. console.log(arr instanceof Object);
  7. </script>
  8. ...

类数组,”类”是类似的类。

  1. ...
  2. <script>
  3. ...
  4. const light = {
  5. 0:'red',
  6. 1:'green',
  7. 2:'blue',
  8. length:3,
  9. };
  10. console.log(light);
  11. </script>
  12. ...

访问方法也可以是一样的

  1. ...
  2. <script>
  3. ...
  4. console.log(arr[0], arr[1], arr[2]);
  5. console.log(light[0], light[1], light[2]);
  6. ...
  7. </script>
  8. ...

类数组在DOM中经常被用到。

二、获取dom元素的API及使用场景

1. querySelectorAll

得到一组元素并可针对性修改。

  1. ...
  2. <script>
  3. ...
  4. const items = document.querySelectorAll('.list .item');
  5. console.log(items);
  6. // items是一个NodeList,节点列表对象,有自己的方法。
  7. console.log(items.keys());
  8. items.forEach(v => (v.style.backgroundColor = 'yellow'));
  9. ...
  10. </script>
  11. ...

2. qureySelector

获取一组元素的第一个

  1. ...
  2. <script>
  3. ...
  4. const lis = document.querySelector('.item');
  5. console.log(lis);
  6. lis.style.backgroundColor = 'violet';
  7. ...
  8. </script>
  9. ...

3. 快捷方式

  1. ...
  2. <script>
  3. ...
  4. // 传统方式
  5. console.log(document.querySelector('body'));
  6. console.log(document.querySelector('head'));
  7. console.log(document.querySelector('title').textContent);
  8. // 快捷方式
  9. console.log(document.body);
  10. console.log(document.head);
  11. console.log(document.title);
  12. ...
  13. </script>
  14. ...

三、优雅的获取form表单元素与控件的值

  1. ...
  2. <form action="login.php" method="post" id="login">
  3. <fieldset class="login">
  4. <legend>用户登录</legend>
  5. <label for="email">邮箱:</label>
  6. <input type="email" name="" id="email" value="admin@php.cn" autofocus>
  7. <label for="password">密码:</label>
  8. <input type="password" name="" id="password" value="123456">
  9. <button>提交</button>
  10. </fieldset>
  11. </form>
  12. <script>
  13. ...
  14. // 表单:form.id
  15. console.log(document.forms.login);
  16. // 表单控件:input.name
  17. console.log(document.forms.login.email);
  18. // 表单控件的值:input.name.value
  19. console.log(document.forms.login.email.value);
  20. ...
  21. </script>
  22. ...

四、DOM元素的遍历与常用API

节点类型包括:
window:全局对象
document:文档对象,即HTML文件
element:元素对象,ul/li/table…
text:文本对象,item1,item2…

  1. <ul class="list">
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3</li>
  5. <li class="item">item4</li>
  6. <li class="item">item5</li>
  7. <li class="item">item6</li>
  8. </ul>
  9. ...
  10. <script>
  11. ...
  12. let ul = document.querySelector('.list');
  13. console.log(ul.children);//类数组
  14. // 转成真数组
  15. console.log(Array.from(ul.children));
  16. // 更优雅的方法
  17. console.log([...ul.children]);
  18. [...ul.children].forEach(li =>(li.style.color = 'blue'));
  19. [...ul.children][0].style.backgroundColor = 'yellow';
  20. ul.firstElementChild.style.backgroundColor = 'red';
  21. // 第二个
  22. ul.firstElementChild.nextElementSibling.style.backgroundColor = 'green';
  23. // 最后一个
  24. ul.lastElementChild.style.backgroundColor = 'red';
  25. // 最后一个的前一个
  26. ul.lastElementChild.previousElementSibling.style.backgroundColor = 'pink';
  27. // 父节点
  28. ul.lastElementChild.parentElement.style.border = '2px solid red';
  29. ...
  30. </script>
  31. ...

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