There can be no Triumph without Loss,No Victory without Suffering,No Freedom without Sacrifice.
All you have to decide is what to do with the time that is given to you.
Get busy Living, or Get busy Dying?
  首页 | 留言给我 | 订阅 Rss | CLI | 黄白之恋 Posts:158   Hits: 5011761    Comments: 173    
 日历归档
<<  <  2024 - 03  >  >>
SuMoTuWeThFrSa
     12
3456789
10111213141516
17181920212223
24252627282930
31
 About Me
 Name: ZhangSichu
 Sex: Male
 Age: 32
 Email: ZhangSichu@gmail.com
 MSN: ZhangSichu@hotmail.com
 Home: ZhangSichu.com
 WeiBo: weibo.com/zhangsichu
 个人推荐
 分类归档
  ·C++/C(5)  RSS
  ·软件工程(1)  RSS
  ·杂事/随感(26)  RSS
  ·.Net/Java(30)  RSS
  ·面向对象程序设计(5)  RSS
  ·汇编/破解(0)  RSS
  ·平面设计(3)  RSS
  ·SQL(5)  RSS
  ·COM/COM+(2)  RSS
  ·Web开发(81)  RSS
 My Friends
Back Forward Refresh Home 2024年3月29日 星期五 RSS CLI Mine Sweeper. In Javascript.

  javascript 遍历Dom 对象
字体大小 [ ]

遍历某个页面的Dom对象。例如想知道某个DOM对象是否在获得焦点状态。遍历各个DOM节点判断document.activeElement.id == node.id。来确定对象是否获得焦点。
一般会使用各个node访问。类似下面的写法。并且这种写法IE FF都支持。
可是当Dom元素很多的情况下这种方法会很慢。
function IterateNode(node){
//do some thing here.
var childrens = node.childNodes;
if(childrens == null ||
typeof(childrens) == "undefined" ||
childrens.length == 0) return;
for(var i=0;i<childrens.length;i++){
IterateNode(childrens[i]);
}
}


在DOM Level 2标准中提供了两个比较好的方法:
document.createNodeIterator
document.createTreeWalker
createTreeWalker 的功能比 createNodeIterator 强大。支持在兄弟节点上交互(nextSibling previousSibling parentNode)。支持取得首个和最后一个Dom对象(firstChild lastChild)。但是只有FF支持了。用这种方法遍历Dom对象比较快。
function ShowAllDomsByTreeWalker(){
var result;
var walker = document.createTreeWalker(document,NodeFilter.SHOW_ELEMENT,null,false);
var node = walker.nextNode();
while(node){
result += node.tagName + "\t" + node.value + "\t" + node.href + "\n";
node = walker.nextNode();
}
//OutputAllDoms(result);
//walker.nextSibling();
//...
}
  Posted @ 4/13/2007 7:01:07 PM | Hits (70377) | Comments (1

  Comment
 #re:javascript 遍历Dom 对象  7/9/2007 4:16:03 PM  billllll
不知javascript能不能获取到网页的<head>和<title>里的东西?
  Post Comment
标题 *
作者 *
密码 记住我
评论 *
    


Stable in Firefox 1.5 2.0Stable in IE6 IE7Stable in MozillaStable in Netscape
ZhangSichu.com V0.1.7507
Powered By ZhangSichu
Copyright © ZhangSichu
Download ZhangSichu.com source code. Download source code