为什么一个DIV只有一个孩子,却有三个孩子节点?

编码兔子

我有两个股利看起来像:

<div id="responseframe">
    <div id="oldframe"> 
    </div>
</div>

我以为#oldframe DIV是#responseframe的唯一孩子。但是,当我用JavaScript编写此代码时,

    var old=document.getElementById("responseframe");
    var nodesnumber=old.childNodes.length;
    console.log("-------------Here is the nodes number of reponseframe---------: "+nodesnumber);
    var nodesname=old.childNodes[i].nodeName;
    console.log("-------------Here is the nodes name of reponseframe's child---------: "+nodesname);

控制台告诉我#responseframe有3个子节点,并且

  1. childNode [0]是#text;
  2. childNode [1]是DIV;
  3. childNode [2]是#text

为什么有2个#text?谢谢你的任何想法。

凯伦·格里高良(Karen Grigoryan)

Node.childNodes 属性返回父元素的所有直接子元素elementNode,包括textNodes,commentNodes。

因此,您有:

<div id="responseframe"><!-- this whole space area is considered by html as single space so you got your first #text Node
    --><div id="oldframe"><!-- this area is ignored because this is not direct child area of the responseframe
    --></div><!-- this whole space area is considered by html as single space so you got your second #text Node
--></div>

所以最后我们有了直接的孩子:#text0 #DIV(oldframe) #text1

如果只想获取直接elementNode(没有textNode和commentNodes),则需要Node.children

var old=document.getElementById("responseframe");
    var nodesnumber=old.children.length;
    console.log("-------------Here is the nodes number of reponseframe---------: "+nodesnumber);
    var nodesname=old.children[i].nodeName;
    console.log("-------------Here is the nodes name of reponseframe's child---------: "+nodesname);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

删除只有一个孩子的分支节点

当只有一个孩子时,为什么“ this.props.children”不是数组?

Scrollview只有一个孩子,但有例外

React-Router只有一个孩子

Javascript 递归:删除只有一个孩子的级别

为什么一个孩子的 div 推着另一个孩子在它下面?

CSS:有班级的孩子的第一个孩子

为什么一个孩子的div不能达到其他孩子的高度?

为什么模式匹配一个单词却有两个相同的单词?

为什么我收到警告“遇到两个孩子使用相同的密钥,‘1’。” 即使数组中只有一个元素

我需要选择(不是最后一个孩子)只有当它有一个特定的类

为什么这三个 ggsurvplots 没有合并为一个?

为什么身体的第一个孩子没有定义?

仅在父母中有另一个孩子时给一个孩子定型

Flex栏,只有一个具有Flex顺序的孩子

div的第一个孩子

与孩子复制一个div

如何检查三个变量中是否只有一个不为空?

断言只有一个孩子在`this.props.children`中传递

Laravel Eloquent-从只有一个孩子的父母那里获得结果

在IE 10/11中,react-redux提供程序给出错误“必须为onlyChild传递一个只有一个孩子的孩子”

为什么只有一个按钮?

为什么只有一个警告循环?

为什么每个文件只有一个类

为什么此查询只有一个结果?

为什么只有最后一个输入是存储?

选择一个孩子

选择没有指定班级的最后一个孩子

jQuery找到第一个有孩子的父母