我的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;
}"
<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;
}"
解决方案 »
- js放大镜或css样式放大镜
- 如何判断链接内容是否有效?链接的内容较大
- innerHTML‘这儿的js代码不能执行,跪求解释’;
- jqery问题, $(document).ready(function(){$("#<%=Button1.ClientID%>").缺少对象
- 再发一贴,200分求教,求助数据提取问题
- 菜鸟悲壮的来了。jquery验证用户名。
- jquery新手问题4
- 高分求一个javascript实现窗口最小化的代码?等待..................
- 请教完美动态下拉列表框如何实现数据的接送?工作中,急,请帮忙!
- 能不能用JavaScript在客户端调用windows画图程序转换图片格式,jpg转png
- js 里用Hashtable时的问题
- 怎么在图片下加个格导航【1】【2】【3】【4】【5】??
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的
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);
}
的value属性有值的时候就找不到报undefine的错, value为空就可以找到,我都绝望了.不知道是什么原因
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);
}
}
}
}这个你觉得有问题吗?
为什么要循环呢~~而且你的结构很乱。。
你直接说你想做什么吧
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>
alert(m+": " + row.cells[m].childNodes[1].tagName); //当找到第4列格子里面的第2个标签的时候,js就提示undefine;
你看看,你前面用children,后面取的时候childNodes
用jQuery可以
if(childeNodes[i].tagName.toLowerCase()=="a")
var obj=[];
obj.push(childeNodes[i].nodeValue);
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
}