Walking the DOM

DOM提供很多API,在相应操作中,选择最高效的API.

一、用childNodes获得元素集合,再遍历

二、用nextSibling来获取每个相邻元素,  firstChild + nextSibling 加do while   

写法

var el = document.getElementById('mydiv'),

  ch = el.firstChild,

  name = '';

do{

  name = ch.nodeName;

} while( ch = ch.nextSibling);          while ch = ch.nextSibling        

nextSibling 在ie6中比childNodes快16倍,在ie7中快105倍

元素节点

遍历中要过滤掉文本节点,而childNodes中却有这样的。

新API中有只获取元素节点的方法

children    ie6也有

childElementCount

firstElementChild

lastElementChild

nextElementSibling

previousElementSibling

childre比childNodes快很多.

相关文章:

  • 2022-02-10
  • 2021-08-22
  • 2021-12-16
  • 2021-07-16
  • 2022-12-23
  • 2021-11-28
猜你喜欢
  • 2021-11-08
  • 2022-12-23
  • 2021-11-17
  • 2021-11-17
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案