Blogger Information
Blog 28
fans 0
comment 0
visits 11632
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取元素的api及dom常用的遍历方法
子墨吖ฅฅ
Original
318 people have browsed it

JS代码部分

  1. // 获取item的所有li标签
  2. let list = document.querySelectorAll('.item > *')
  3. //给所有li标签字体颜色改为红色 (第一种遍历方法)
  4. list.forEach(li => (li.style.color = 'red'))
  5. // 单独设置第一个li标签的字体为粉色(第一种方法)
  6. let child = document.querySelector('.item > *')
  7. child.style.color = 'pink'
  8. // 使用第二种方法进行获取子节点
  9. list = document.querySelector('.item')
  10. // 单独设置最后一个li标签的字体为蓝色(第二种方法)
  11. let last = list.lastElementChild
  12. last.style.color = 'blue'
  13. //遍历给所有li标签加上边框(第二种遍历方法)
  14. ;[...list.children].forEach(item => (item.style.border = '1px solid black'))

HTML代码部分

  1. <ul class="item">
  2. <li>text1</li>
  3. <li>text2</li>
  4. <li>text3</li>
  5. <li>text4</li>
  6. <li>text5</li>
  7. <li>text6</li>
  8. </ul>

运行图

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