javaScript初学的一个问题,得不到nodeValue的值 javascriptjava 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 document.getElementById("inputSubmit").onclick=function(){ var kechengs = document.getElementsByName("kecheng"); var kechengArray = new Array(); for (var i = 0; i < kechengs.length; i++) { if (kechengs[i].checked) { alert("i : " + i); alert("node name: " + kechengs[i].parentNode.parentNode.children[0].nodeName); alert("node type: " + kechengs[i].parentNode.parentNode.children[0].nodeType); alert("node value: " + kechengs[i].parentNode.parentNode.children[0].nodeValue); alert("node value: " + kechengs[i].parentNode.parentNode.children[0].id); kechengArray.push(kechengs[i].parentNode.parentNode.childNodes[0].nodeValue); } } }; 用 children[0] 也是同样的错误,不过如果用children[1]时就有点不同了,3 的结果会是 node value : null 你是要获取到已经被勾上的课程的id吗?那直接在checkbox上加上value,<c:forEach items="${xuekeinfoall.xuekeinfoalllist }" var="xueke"> <tr class="trClass" id="tr2"> <td class="tdClass2" id="kechengid">zhong${xueke.xuekeinfo.kechengid }</td> <td class="tdClass2" id="2">guo${xueke.xuekeinfo.kechengname }</td> <td class="tdClass2" id="3">${xueke.teacher.teachername }</td> <td class="tdClass2" id="4">${xueke.xuekeinfo.kaikexueyuan }</td> <td class="tdClass2" id="5">${xueke.xuekeinfo.studentmaxcount }</td> <td class="tdClass2" id="6">${xueke.xuekeinfo.studentcurrentcount }</td> <td class="tdClass2" id="7"><input name="kecheng" type="checkbox" id="kecheng" value="${xueke.xuekeinfo.kechengid }"/></td> </tr> </c:forEach>document.getElementById("inputSubmit").onclick=function(){ var kechengs = document.getElementsByName("kecheng"); var kechengArray = new Array(); for (var i = 0; i < kechengs.length; i++) { if (kechengs[i].checked) { //alert("i : " + i); //alert("node name: " + kechengs[i].parentNode.parentNode.children[0].nodeName); //alert("node type: " + kechengs[i].parentNode.parentNode.children[0].nodeType); //alert("node value: " + kechengs[i].parentNode.parentNode.children[0].nodeValue); //alert("node value: " + kechengs[i].parentNode.parentNode.children[0].id); kechengArray.push(kechengs[i].value); } } };这样就可以了哇。你费那么大的劲,何必呢 nodeValue是针对属性节点的。文本节点和元素节点没法用nodeValue获取到里面的内容。取到对象后用innerHTML获取里面内容吧 我在网上查的可以这样写。我用innerHTML也不行,关键是我那个 id 的结果是 undefined 我在网上查的可以这样写。我用innerHTML也不行,关键是我那个 id 的结果是 undefined你那个foreach是不是循环那个tr啊如果是你的id就重复了所以就是undefined了 写这么详细 很好#text 这个节点比较特殊不属于dom节点你的本意是要获取dom节点但是xml格式特性 文本也是一个节点 但是不是dom节点你 input 的 parent 的 parent 是<tr class="trClass" id="tr2">这个节点 下面的第一个子元素 里面是空格 会解释成 一个文本节点所以不是你要的东西 你应该判断 nodeType == 1 在执行你的 alert操作 这是个好办法啊,谢谢啊。就是不知道我这个为什么不能用因为很多情况下firstchild会是一个空格,没有任何意义的空格,造成的原因有很多,比如编辑器里格式化的时候。 我在网上查的可以这样写。我用innerHTML也不行,关键是我那个 id 的结果是 undefined你那个foreach是不是循环那个tr啊如果是你的id就重复了所以就是undefined了\我这个alert("node value: " + kechengs[i].parentNode.parentNode.firstChild.id);不是已经定位到了我要的那个 tr 中的第一个 td 上了吗,id 不是就是我想要的那个 td 的一个属性? 这是个好办法啊,谢谢啊。就是不知道我这个为什么不能用因为很多情况下firstchild会是一个空格,没有任何意义的空格,造成的原因有很多,比如编辑器里格式化的时候。这个我用 children[0] 也是相同的错误 我现在把 firstChild 换成 children[0] 后的结果(前两天的测的这两个结果是一样的)是:node name:TDnode type:1node value:nullnode value:kechengid我第一个 td 中有文本内容。如果按照你说的文本也是结点,那就在 .children[0] 后再加一个 .children[0] 成alert("node name: " + kechengs[i].parentNode.parentNode.children[0].children[0].nodeName);alert("node type: " + kechengs[i].parentNode.parentNode.children[0].children[0].nodeType);alert("node value: " + kechengs[i].parentNode.parentNode.children[0].children[0].nodeValue);alert("node value: " + kechengs[i].parentNode.parentNode.children[0].children[0].id);结果什么也没输出,说明是错误的 kechengs[i].parentNode.parentNode.firstChild 修改为kechengs[i].parentNode.parentNode.firstElementChild 我在网上查的可以这样写。我用innerHTML也不行,关键是我那个 id 的结果是 undefined你那个foreach是不是循环那个tr啊如果是你的id就重复了所以就是undefined了\我这个alert("node value: " + kechengs[i].parentNode.parentNode.firstChild.id);不是已经定位到了我要的那个 tr 中的第一个 td 上了吗,id 不是就是我想要的那个 td 的一个属性?node value: " + kechengs[i].parentNode.parentNode这句话没有定位到td上而是定位到tr和td之间的空文本上了。你前面获取都是文本节点吗!元素节点nodeType为1属性节点nodeType为2文本节点nodeType为3将你的第四行代码紧贴在第三行代码后面(tr后面紧贴td)不要有空格你就可以获取到id属性了 你的firstChild获取的不是你想要的td,而是一个text元素,也就是说是<tr> <td>之间的空格字符串,这个字符串在DOM模型里也是作为child出现的。从而导致和你预想的记过不一致。 具体语句可以这么写:kechengs[i].parentNode.parentNode.getElementsByName('td')[0].id 谢谢各位哈,我明白了,要得到我想要的正确的写法这样alert("node name: " + kechengs[i].parentNode.parentNode.children[0].firstChild.nodeName);alert("node type: " + kechengs[i].parentNode.parentNode.children[0].firstChild.nodeType);alert("node value: " + kechengs[i].parentNode.parentNode.children[0].firstChild.nodeValue);children[0] 也可以换成 firstElementChildchildren[0] 和 firstElementChild 代表第一个元素节点firstChild 代表第一个节点,在我这个问题这里是 tr 与 td 之间的空格,如果没有这个空格也是正确的 也可以这样写alert("node value: " + kechengs[i].parentNode.parentNode.children[0].innerHTML);//或alert("node value: " + kechengs[i].parentNode.parentNode.getElementsByTagName("td")[0].innerHTML); 无法修改学生号,怎么办 新手学Hibernate!高手请进来~~ 大家现在都用什么框架 hibernate hql脚本中数据类型转换问题 求助Struts+Spring+Hibernate 框架插不进值进数据库 在tomcat下开发的struts可以在Weblogic下运行吗 一个初学ejb的问题 求,JBJ7开发EJB,尤其是CMP,BMP方面比较深入的书籍! 推荐一个实用的 ejb 服务器吧 一个奇怪的问题,你们猜猜System.out.println(0.1+0.1+0.1);输出多少。 请教大大们一个问题,本人初学Servlet 关于JavaMail发送邮件被对方服务器退回的问题
var kechengs = document.getElementsByName("kecheng");
var kechengArray = new Array();
for (var i = 0; i < kechengs.length; i++) {
if (kechengs[i].checked) {
alert("i : " + i);
alert("node name: " + kechengs[i].parentNode.parentNode.children[0].nodeName);
alert("node type: " + kechengs[i].parentNode.parentNode.children[0].nodeType);
alert("node value: " + kechengs[i].parentNode.parentNode.children[0].nodeValue);
alert("node value: " + kechengs[i].parentNode.parentNode.children[0].id);
kechengArray.push(kechengs[i].parentNode.parentNode.childNodes[0].nodeValue);
}
}
};
<c:forEach items="${xuekeinfoall.xuekeinfoalllist }" var="xueke">
<tr class="trClass" id="tr2">
<td class="tdClass2" id="kechengid">zhong${xueke.xuekeinfo.kechengid }</td>
<td class="tdClass2" id="2">guo${xueke.xuekeinfo.kechengname }</td>
<td class="tdClass2" id="3">${xueke.teacher.teachername }</td>
<td class="tdClass2" id="4">${xueke.xuekeinfo.kaikexueyuan }</td>
<td class="tdClass2" id="5">${xueke.xuekeinfo.studentmaxcount }</td>
<td class="tdClass2" id="6">${xueke.xuekeinfo.studentcurrentcount }</td>
<td class="tdClass2" id="7"><input name="kecheng" type="checkbox" id="kecheng" value="${xueke.xuekeinfo.kechengid }"/></td>
</tr>
</c:forEach>
document.getElementById("inputSubmit").onclick=function(){
var kechengs = document.getElementsByName("kecheng");
var kechengArray = new Array();
for (var i = 0; i < kechengs.length; i++) {
if (kechengs[i].checked) {
//alert("i : " + i);
//alert("node name: " + kechengs[i].parentNode.parentNode.children[0].nodeName);
//alert("node type: " + kechengs[i].parentNode.parentNode.children[0].nodeType);
//alert("node value: " + kechengs[i].parentNode.parentNode.children[0].nodeValue);
//alert("node value: " + kechengs[i].parentNode.parentNode.children[0].id);
kechengArray.push(kechengs[i].value);
}
}
};这样就可以了哇。你费那么大的劲,何必呢
取到对象后用innerHTML获取里面内容吧
你那个foreach是不是循环那个tr啊如果是你的id就重复了所以就是undefined了
你的本意是要获取dom节点
但是xml格式特性 文本也是一个节点 但是不是dom节点你 input 的 parent 的 parent 是<tr class="trClass" id="tr2">这个节点 下面的第一个子元素 里面是空格 会解释成 一个文本节点
所以不是你要的东西 你应该判断 nodeType == 1 在执行你的 alert操作
就是不知道我这个为什么不能用因为很多情况下firstchild会是一个空格,没有任何意义的空格,造成的原因有很多,比如编辑器里格式化的时候。
你那个foreach是不是循环那个tr啊如果是你的id就重复了所以就是undefined了\
我这个alert("node value: " + kechengs[i].parentNode.parentNode.firstChild.id);不是已经定位到了我要的那个 tr 中的第一个 td 上了吗,id 不是就是我想要的那个 td 的一个属性?
就是不知道我这个为什么不能用因为很多情况下firstchild会是一个空格,没有任何意义的空格,造成的原因有很多,比如编辑器里格式化的时候。这个我用 children[0] 也是相同的错误
node name:TD
node type:1
node value:null
node value:kechengid
我第一个 td 中有文本内容。如果按照你说的文本也是结点,那就在 .children[0] 后再加一个 .children[0] 成alert("node name: " + kechengs[i].parentNode.parentNode.children[0].children[0].nodeName);
alert("node type: " + kechengs[i].parentNode.parentNode.children[0].children[0].nodeType);
alert("node value: " + kechengs[i].parentNode.parentNode.children[0].children[0].nodeValue);
alert("node value: " + kechengs[i].parentNode.parentNode.children[0].children[0].id);
结果什么也没输出,说明是错误的
修改为
kechengs[i].parentNode.parentNode.firstElementChild
你那个foreach是不是循环那个tr啊如果是你的id就重复了所以就是undefined了\
我这个alert("node value: " + kechengs[i].parentNode.parentNode.firstChild.id);不是已经定位到了我要的那个 tr 中的第一个 td 上了吗,id 不是就是我想要的那个 td 的一个属性?
node value: " + kechengs[i].parentNode.parentNode这句话没有定位到td上而是定位到tr和td之间的空文本上了。
你前面获取都是文本节点吗!
元素节点nodeType为1
属性节点nodeType为2
文本节点nodeType为3
将你的第四行代码紧贴在第三行代码后面(tr后面紧贴td)不要有空格你就可以获取到id属性了
alert("node type: " + kechengs[i].parentNode.parentNode.children[0].firstChild.nodeType);
alert("node value: " + kechengs[i].parentNode.parentNode.children[0].firstChild.nodeValue);children[0] 也可以换成 firstElementChild
children[0] 和 firstElementChild 代表第一个元素节点
firstChild 代表第一个节点,在我这个问题这里是 tr 与 td 之间的空格,如果没有这个空格也是正确的
//或
alert("node value: " + kechengs[i].parentNode.parentNode.getElementsByTagName("td")[0].innerHTML);