dom中的children对象数组元素firstChild,lastChild的使用

 

ExpandedBlockStart.gif 代码
<!-- children对象数组元素示例  -->
< html >  
< body >  
  
< table    id ="tbl" >    
  
< tbody >   < tr >   < td >  行1列1  </ td >< td > 行1列2  </ td ></ tr >    
  
< tr >   < td >  行2列1  </ td >< td > 行2列2  </ td ></ tr >  
  
</ tbody >  
  
</ table >    
  
< input    onclick ="alert(document.all.tbl.children(0).children(0).innerHTML)"    value ="children(0)"    type ="button" >    
  
< input    onclick ="alert(document.all.tbl.children(0).children(1).innerHTML)"    value ="children(1)"    type ="button" >    
  
< input    onclick ="alert(document.all.tbl.children(0).children(1).children(0).innerHTML)"    value ="children(2)"    type ="button" >  
  
< div    id =test >    
  
< a ></ a >    
  
< table ></ table >    
  
</ div >    
    
  
< script >    
  alert(test.children[
0 ].tagName)   
  alert(test.children[
1 ].tagName)   
  
</ script >    
  
</ body >  
</ html >  

<!-- 在DOM中,整个页面里的元素是个树型结构 
children 代表对象的子节点数组
-->

document.all.tbl.children(0).children(0).innerHTML解释 
document.all.tbl.children(0)定位到table的第一个子节点
< tbody >  
document.all.tbl.children(0).children(0)定位到table的第一个子节点的第一个子节点
< tr > ,打印结果 < td >  行1列1  </ td >< td > 行1列2  </ td >  


document.all.tbl.children(0).children(1).innerHTML,打印结果
< td >  行2列1  </ td >< td > 行2列2  </ td >  


document.all.tbl.children(0).children(1).children(0).innerHTML 打印结果行2列2 


<!-- fistChild,lastChild示例 -->  
< html >  
< body >  
  
< table    id ="tbl" >    
  
< tbody >   < tr  id ="tr1" >   < td >  行1列1  </ td >< td > 行1列2  </ td ></ tr >    
  
< tr >   < td ="tr2" >  行2列1  </ td >< td > 行2列2  </ td ></ tr >  
  
</ tbody >  
  
</ table >    
  
< input    onclick ="alert(document.all.tbl.firstChild.firstChild.innerHTML)"    value ="children(0)"    type ="button" >    
  
< input    onclick ="alert(document.all.tbl.firstChild.lastChild.innerHTML)"    value ="children(1)"    type ="button" >    
  
< input    onclick ="alert(document.all.tbl.firstChild.lastChild.firstChild.innerHTML)"    value ="children(2)"    type ="button" >  
  
< div    id =test >    
  
< a ></ a >    
  
< table ></ table >    
  
</ div >    
    
  
< script >    
  alert(test.firstChild.tagName)   
  alert(test.lastChild.tagName)   
  
</ script >    
  
</ body >  
</ html >  

document.all.tbl.firstChild定位到table的第一个子节点
< tbody >  
document.all.tbl.firstChild.firstChild定位到table的第一个子节点
< tbody >  
的第一个子节点
< tr  id ="tr1" >  
document.all.tbl.firstChild.firstChild.innerHTML打印结果是
< td >  行1列1  </ td >< td > 行1列2  </ td >  


document.all.tbl.firstChild.lastChild定位到table的第一个子节点
< tbody >  
的最后子节点
< tr  id ="tr2" >  
document.all.tbl.firstChild.lastChild.innerHTML打印结果是
< td ="tr2" >  行2列1  </ td >< td > 行2列2  </ td >  


document.all.tbl.firstChild.lastChild.firstChild.innerHTML 打印结果是 
行2列2 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值