日期:2014-05-16  浏览次数:20370 次

如何通过JS 动态得到嵌套TABLE中TD的内容
如何通过JS   动态得到嵌套TABLE中TD的内容,例如:
<table>
    <tbody   id= "table1 ">
    //这里做了个循环,是对以下的行进行迭代
    <tr
          <td>
                <table>
                      <tr>
                            <td>
                                      123
                            </td>
                      </tr>
                      <tr>
                            <td>
                                      456
                            </td>
                      </tr>
                        </table>
                      </td>
                    </tr>
//循环结束
                  </tbody>
                  </table>    

说明一下,我用的是document.getElementById( "table1 ").rows[i].cells[0].innerText
因为我要得到 "123 "的结果,但是得到的却是 "123   456 ",请各位帮小弟看看~谢谢~

------解决方案--------------------
document.getElementById( "table1 ").rows[i].cells[0].childNodes[0]是里面的那个table

然后你再用rows[i].cells[0]吧
------解决方案--------------------
document.getElementById( "table1 ").rows[i].cells[0].innerText的作用是显示table1中的第i-1行、第1列的显示内容,所以找不到table1.rows[i].cells[0]中的所有显示内容,所以是123 456。你要设法得到table1.rows[i].cells[0]里面的table的内容。
------解决方案--------------------
用childNodes可以一致嵌套到最里层
document.getElementById( "table1 ").rows[i].cells[0].childNodes[0].childNodes[0].childNodes[0]...
一直到你想获得的地方