Blogger Information
Blog 42
fans 0
comment 0
visits 15341
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0725自定义类数组,dom元素的API有几个 ,dom元素的遍历与常用API有哪些
小言
Original
379 people have browsed it

自定义类数组

分为数组和类数组
数组其实也是以对象的方式所存在的。
  1. const colors = ['red', 'green', 'blue'];
  2. console.log(colors);

类数组,就是一个对象的字面量
  1. const animals = {
  2. 0: 'dog',
  3. 1: 'cat',
  4. 2: 'pig',
  5. length: 3,
  6. };
  7. console.log(animals);

dom元素

dom元素,常用的有以下二种
querySelectorAll () 代表一组

就好像css选择器,可以针对多个一起选择

  1. const items = document.querySelectorAll('.list .item');
  2. console.log(items);

querySelector () 代表一个
  1. const eles = document.querySelectorAll('.list .item:first-of-type');
  2. console.log(eles);
  3. console.log(eles[0]);
  4. eles[0].style.backgroundColor = 'yellow';

dom元素的遍历

从选择到的第N层的算元素,然后进行一层一层的选择,如果存在返回 上一层,当不存在时返回 最开始的一层

  1. ul.firstElementChild.style.backgroundColor = 'red';
  2. ul.firstElementChild.nextElementSibling.style.backgroundColor = 'green';
  3. ul.lastElementChild.style.backgroundColor = 'red';
  4. ul.lastElementChild.previousElementSibling.style.backgroundColor = 'pink';

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!