+ -
当前位置:首页 → 问答吧 → 求助,children()方法遇到的一些问题

求助,children()方法遇到的一些问题

时间:2009-10-09

来源:互联网

测试代码:
<table id=t1>
<tr>
        <Td class=d1>Hello1</td>
</tr>
<tr class=d2> 
        <Td>Hello2</td>
</tr>
<tr>
        <Td>Hello3</td>
</tr>
<tr>
        <Td>Hello4</td>
</tr>
<tr> 
        <Td>Hello5</td>
</tr>

</table>

1. alert($('#t1').children().size()) 结果为1
alert($('#t1 tr').size()) 结果为5
这两者有什么区别呢?

2. alert($('tr').children('.d1').text()) 结果 Hello1
    alert($('#t1').children('.d1').text()) 结果 空         ??
 这里我也不太明白 children('**') 是只遍历 对应的直接子元素吗?二层儿子节点就没有遍历到?
    alert($('#t1').children('.d2').html()) 结果 null ,     ??
 这句就是查找直接子元素,为什么还是为null
3. alert($('#t1').children().children().text()) 结果Hello1Hello2Hello3Hello4Hello5
   alert($('#t1').children().children().html())  结果空 
这又是什么原因呢?

  望高手指教!!

作者: js0218   发布时间: 2009-10-09

问题1:table的子集为tbody而不是tr
              $('#t1 tr') == $('#t1>tbody>tr')

问题2:先学习一下DOM tree
问题3:jquery返回的始终是个collection,同问题1,
              把代码改成alert($('#t1').children().children().parent().html()) 你就明白为什么错了
          
        

作者: zcrane   发布时间: 2009-10-09

相关阅读 更多