我的table表结构是这样的:<table id="tb1">
<tr align="center">
<td>
<input type="checkbox" value='66' name="checkon"/>
</td><td style="width:40px;">
      1
      </td><td style="width:100px;">dddd</td><td style="width:120px;">
                     <span id="stp66" style="display:inline;"></span>
                     <div id="divtp66" style="display:none">dadfasdf</div>
               </td><td style="width:200px;">
                  <span id="sbt66" style="display:inline;">dfsdfsdfasdasd</span>
                  <input  id="tbt66" type="text" maxlength="100" style="width:215px;display:none;" value="dfsdfsdfasdasd"/>
               </td><td style="width:80px;">
                  <span id="srq66" style="display:inline;">2010-10-24</span>
                  <input id="trq66" maxlength="10" type="text" onclick="WdatePicker();" style="width:65px;display:none;" value="2010-10-24" onkeypress="return !(event.keyCode != -1);"/>
               </td><td style="width:60px;">
                  <span id="szz66" style="display:inline;"></span>
                  <input id="tzz66" type="text" maxlength="20" style="width:55px;display:none;" value=""/>
               </td><td style="width:50px;">
                  <span id="sbq66" style="display:inline;"></span>
                  <input id="tbq66" type="text" maxlength="10" style="width:48px;display:none;" value=""/>
               </td><td style="width:100px;">
                  <span id="slj66" style="display:inline;">http://www.baidu.com</span>
                  <input id="tlj66" type="text" maxlength="100" style="width:95px;display:none;" value="http://www.baidu.com"/>
               </td><td style="width:60px;">
                  <span id="sdj66" style="display:inline;">122</span>
                  <input id="tdj66" type="text" maxlength="8" style="width:55px;display:none;" value="122"/>
               </td><td style="width:60px;">
                  <span id="spl66" style="display:inline;">0</span>
                  <input id="tpl66" type="text" maxlength="8" style="width:55px;display:none;" value="0"/>
               </td><td style="width:55px;">
                  <span id="sdj66" style="display:inline;">34</span>
                  <input id="tdj66" type="text" maxlength="3" style="width:30px;display:none;" value="34"/>
               </td> </tr>
</table>
目标: 遍历,找到第4列格子里面的第2个标签控件"<input  id="tbt66" type="text" maxlength="100" style="width:215px;display:none;" value="dfsdfsdfasdasd"/>可是我只能 找到第4列格子里面的第1个标签<span>控件, 而第2个标签却找不到,js报错: undefine, 不知道是什么原因,跪求!!!!!我的代码如下:        for (var m = 2; m < row.cells.length; m++) {//row代表给定那行对象
            for (var n = 0; n < row.cells[m].children.length; n++) {//row.cells[m].children.length这里显示是有两个子控件的啊
                alert(m+": " + row.cells[m].childNodes[1].tagName); //当找到第4列格子里面的第2个标签的时候,js就提示undefine;
            }"

解决方案 »

  1.   

    用你的代码做的测试    window.onload = function() {
            var row = document.getElementById("tb1").rows[0];
            var td = row.cells[4];
            for(var i = 0; i < td.childNodes.length; i++)   {
                if(td.childNodes[i].nodeType == 1) {
                    alert(td.childNodes[i].tagName)
                }
            }
        };提示你下,childNodes包含文本节点和元素节点的
    你需要判断nodeType的
      

  2.   

    高手加了这句,还是找不到第二节点, 报undefine的错:
            for (var m = 2; m < row.cells.length; m++) {
                for (var n = 0; n < row.cells[m].children.length; n++) {
                    if (row.cells[m].childNodes[n].nodeType == 1) {
                        alert(m + ": " + row.cells[m].childNodes[1].tagName);
                    }
      

  3.   

    你的语法我彻底看不懂row.cells[m].children.length==>childNodes.lengthrow.cells[m].childNodes[1].tagName==> row.cells[m].childNodes[n].tagName
      

  4.   

    我这边出现的问题是: 当这个"<input id="tbt66" type="text" maxlength="100" style="width:215px;display:none;" value="dfsdfsdfasdasd"/>
    的value属性有值的时候就找不到报undefine的错, value为空就可以找到,我都绝望了.不知道是什么原因
      

  5.   

    //显示或隐藏列数据
    function editOrhiddenGridData(row, ckey) {//row是我在点击行的时候,传递给他的行对象
            for (var m = 2; m < row.cells.length; m++) {
                for (var n = 0; n < row.cells[m].children.length; n++) {
                    if (row.cells[m].childNodes[n].nodeType == 1) {
                        alert(m + ": " + row.cells[m].childNodes[1].tagName);
                    }
                }
            }
       }这个你觉得有问题吗?
      

  6.   

    你是否只需要找你选中行中的这个text文本
    为什么要循环呢~~而且你的结构很乱。。
    你直接说你想做什么吧
      

  7.   

    在下面的代码中会输处元素的name、value、type及tagname,其中text节点的tagname是undefined将HTML DOM中几个容易常用的属性做下记录:nodeName、nodeValue 以及 nodeType 包含有关于节点的信息。
    nodeName 属性含有某个节点的名称。元素节点的 nodeName 是标签名称
    属性节点的 nodeName 是属性名称
    文本节点的 nodeName 永远是 #text
    文档节点的 nodeName 永远是 #document
    注释:nodeName 所包含的 XML 元素的标签名称永远是大写的nodeValue
    对于文本节点,nodeValue 属性包含文本。对于属性节点,nodeValue 属性包含属性值。nodeValue 属性对于文档节点和元素节点是不可用的。nodeType
    nodeType 属性可返回节点的类型。最重要的节点类型是:元素类型 ==》节点类型
    元素element ==》1
    属性attr ==》2
    文本text ==》3
    注释comments ==》8
    文档document ==》 9
    <html>
    <head>
    <script type="text/javascript">
    function cell()
      {
      var rows=document.getElementById('myTable').rows;
     for(var i=0;i<rows.length;i++){
      var row=rows[i];
      for (var m = 2; m < row.cells.length; m++) {
        for (var n = 0; n < row.cells[m].children.length; n++) {
         alert(m+"\ntype:"+row.cells[m].childNodes[n].nodeType +"\nname:"+row.cells[m].childNodes[n].nodeName+"\nvalue:"+row.cells[m].childNodes[n].nodeValue+"\ntagName:"+row.cells[m].childNodes[n].tagName); 
        }
      }
     }
    }
    </script>
    </head>
    <body><table id="myTable" border="1">
    <tr>
     <td>
                                <input type="checkbox" value='66' name="checkon"/>
                            </td><td style="width:40px;">
                              1
                          </td><td style="width:100px;">dddd</td><td style="width:120px;">
                         <span id="stp66" style="display:inline;"></span>
                         <div id="divtp66" style="display:none">dadfasdf</div>
                   </td><td style="width:200px;">
                      <span id="sbt66" style="display:inline;">dfsdfsdfasdasd</span>
                      <input  id="tbt66" type="text" maxlength="100" style="width:215px;display:none;" value="dfsdfsdfasdasd"/>
                   </td><td style="width:80px;">
                      <span id="srq66" style="display:inline;">2010-10-24</span>
                      <input id="trq66" maxlength="10" type="text" onclick="WdatePicker();" style="width:65px;display:none;" value="2010-10-24" onkeypress="return !(event.keyCode != -1);"/>
                   </td><td style="width:60px;">
                      <span id="szz66" style="display:inline;"></span>
                      <input id="tzz66" type="text" maxlength="20" style="width:55px;display:none;" value=""/>
                   </td><td style="width:50px;">
                      <span id="sbq66" style="display:inline;"></span>
                      <input id="tbq66" type="text" maxlength="10" style="width:48px;display:none;" value=""/>
                   </td><td style="width:100px;">
                      <span id="slj66" style="display:inline;">http://www.baidu.com</span>
                      <input id="tlj66" type="text" maxlength="100" style="width:95px;display:none;" value="http://www.baidu.com"/>
                   </td><td style="width:60px;">
                      <span id="sdj66" style="display:inline;">122</span>
                      <input id="tdj66" type="text" maxlength="8" style="width:55px;display:none;" value="122"/>
                   </td><td style="width:60px;">
                      <span id="spl66" style="display:inline;">0</span>
                      <input id="tpl66" type="text" maxlength="8" style="width:55px;display:none;" value="0"/>
                   </td><td style="width:55px;">
                      <span id="sdj66" style="display:inline;">34</span>
                      <input id="tdj66" type="text" maxlength="3" style="width:30px;display:none;" value="34"/>
                   </td>   
    </tr>
    </table>
    <br />
    <input type="button" onclick="cell()" value="点击查看结果"></body>
    </html>
      

  8.   

    for (var n = 0; n < row.cells[m].children.length; n++) {//row.cells[m].children.length这里显示是有两个子控件的啊
      alert(m+": " + row.cells[m].childNodes[1].tagName); //当找到第4列格子里面的第2个标签的时候,js就提示undefine;
    你看看,你前面用children,后面取的时候childNodes
      

  9.   

     $("#tb1 tr td:gt(3)").find("input").val(); 
    用jQuery可以
      

  10.   

    恩 判断下
    if(childeNodes[i].tagName.toLowerCase()=="a")
    var obj=[];
    obj.push(childeNodes[i].nodeValue);
      

  11.   

    for (var m = 2; m < row.cells.length; m++) {//row代表给定那行对象
      for (var n = 0; n < row.cells[m].childrens.length; n++) {//row.cells[m].children.length这里显示是有两个子控件的啊
      alert(m+": " + row.cells[m].childNodes[n].tagName); //当找到第4列格子里面的第2个标签的时候,js就提示undefine;
      }
    childNodes 兼容性不好!ff 谷歌浏览器下 空白也算一个节点!==》
    if(row.cells[m].childNodes[n].tagName){ // 先判断
     alert(m+": " + row.cells[m].childNodes[n].tagName);要不用 getElementsByTagName 来 代替 childNodes
    }