Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML表DOM创建了错误数量的子元素_Html_Dom_Html Table_Children_Tree Nodes - Fatal编程技术网

HTML表DOM创建了错误数量的子元素

HTML表DOM创建了错误数量的子元素,html,dom,html-table,children,tree-nodes,Html,Dom,Html Table,Children,Tree Nodes,我的代码中有一个表,我使用它作为查找表,根据的id从中获取一些值 您认为为什么会发生这种情况?childNodes childNodes 0 <TextNode textContent="\n"> 1 td 2 <TextNode textContent="\n"> 3 td 4 <TextNode textContent="\n"> 5 td 6 <TextNode textContent="\n"> 7 td 8 <

我的代码中有一个表,我使用它作为查找表,根据
的id从
中获取一些值

您认为为什么会发生这种情况?

childNodes
childNodes
0 <TextNode textContent="\n">   
1 td
2 <TextNode textContent="\n">
3 td
4 <TextNode textContent="\n">
5 td        
6 <TextNode textContent="\n">
7 td
8 <TextNode textContent="\n">
0 1 td 2. 3 td 4. 5 td 6. 7 td 8.

我不知道是谁制作了文本节点,浏览器还是javascript解析器,但javascript就是这么看的。

因为标记之间的换行符被算作文本,并被转换成DOM中的文本节点。

childNodes包括所有childNodes,包括文本节点(即单元格之间的空白)。试着这样做:

var children = document.getElementById('nas').childNodes,
    count = 0,
    len = children.length;

for (var i = 0; i < len; i++) {
    // make sure it's type ELEMENT_NODE
    if (children[i].nodeType === 1) { count++ }
}

alert(count); // 4
var children=document.getElementById('nas').childNodes,
计数=0,
len=儿童。长度;
对于(变量i=0;i
这里有一篇很好的文章解释了这个问题:

读者注意:这是一个快速简单的替代
ElementNode.children
集合的方法,该集合支持不稳定的浏览器。
childNodes
0 <TextNode textContent="\n">   
1 td
2 <TextNode textContent="\n">
3 td
4 <TextNode textContent="\n">
5 td        
6 <TextNode textContent="\n">
7 td
8 <TextNode textContent="\n">
var children = document.getElementById('nas').childNodes,
    count = 0,
    len = children.length;

for (var i = 0; i < len; i++) {
    // make sure it's type ELEMENT_NODE
    if (children[i].nodeType === 1) { count++ }
}

alert(count); // 4