javascript - 为什么一个DIV只有一个子节点却有三个子节点?

标签 javascript html

我有两个 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?谢谢你的任何想法。

最佳答案

因为您在 <div id="responseframe"> 之后添加了新行然后先</div> .

如果你把它放在一行中,就会有一个节点:div。

HTML:

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

输出:

-------------Here is the nodes number of reponseframe---------: 1 
-------------Here is the nodes name of reponseframe's child---------: DIV

这是 fiddle :http://jsfiddle.net/cassln/t7kec97u/2/

关于javascript - 为什么一个DIV只有一个子节点却有三个子节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32711741/

相关文章:

css - 如何摆脱 PNG 不可见边框?

html - 如何将容器 div 中的一个子 div 居中?

javascript - 捕获正在执行的 javascript 代码的工具

javascript - Redux-Form matchDispatchToProps 不工作

javascript - 如何在单击按钮之前禁用输入

javascript - Codeigniter:如何从不同的 View 访问 css 文件?

html - 位置 : fixed (but when scrolled down to the fixed content)

html - 当父 div 改变宽度时设置绝对位置

javascript - 在 ASP.NET/C# 中检查复选框服务器控件时隐藏文本框 - 使用 Jquery 或 Javascript

javascript - 如果变量未定义则跳过 promise 之一