在javascript中遍历网页

时间:2011-06-13 14:37:02

标签: javascript

  

可能重复:
  How to iterate through a DOM tree?

你能告诉我如何在javascript中编写以下伪代码吗?

for each child of <body>:
  if (tag.haschildren):
    for each tag.child:
       do some action;

谢谢

1 个答案:

答案 0 :(得分:1)

var els = document.getElementsByTagName("*");
for (var i = 0, ii = els.length; i < ii; i++) {
  for (var j = 0, jj = els[i].childNodes.length; j < jj; j++) {
    action(els[i].childNodes[j]);
  }  
}