额外的文本节点在节点列表中?

huangapple go评论94阅读模式
英文:

Extra text node in nodelist?

问题

I have a section element having 3 div parent elements, I want to log section children.

<section class="section1">
    <div class="parent">
        <p class="childP">hello</p>
    </div>
    <div class="parent">
        <p class="childP">bro
    </div>
    <div class="parent-3">
        <p class="childP">bro
    </div>
</section>
const section = document.querySelector('.section1');

When I log console.log(section.childNodes);, the output is NodeList(7) [text, div.parent, text, div.parent, text, div.parent-3, text]

What is this Extra text in node list?

I tried console.log(section.children) is working completely fine, giving output in Element Nodes, i.e., HTMLCollection. I need the reason for the extra text in section.childNodes node list?

英文:

I have a section element having 3 div parent elements, I want to log section children.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;section class=&quot;section1&quot;&gt;
        &lt;div class=&quot;parent&quot;&gt;
            &lt;p class=&quot;childP&quot;&gt;hello&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class=&quot;parent&quot;&gt;
            &lt;p class=&quot;childP&quot;&gt;bro
        &lt;/div&gt;
        &lt;div class=&quot;parent-3&quot;&gt;
            &lt;p class=&quot;childP&quot;&gt;bro
        &lt;/div&gt;
    &lt;/section&gt;

<!-- end snippet -->

const section = document.querySelector(&#39;.section1&#39;);

when I log, console.log(section.childNodes); the output is NodeList(7)&#160;[text, div.parent, text, div.parent, text, div.parent-3, text]

What is this Extra text in node list ?

I tried console.log(section.children) is woking completely fine giving output in Element Nodes i.e HTMLcollection, I need the reason for extra text in section.childNodes node list?

答案1

得分: 3

以下是已翻译好的部分:

这是关于每个子<div>元素之间的空格。以下是去除了空格的标记,因此在.childNodes中没有Text节点。

console.log(document.querySelector('.section1').childNodes)
<section class="section1"><div class="parent">
    <p class="childP">hello</p>
  </div><div class="parent">
    <p class="childP">bro
  </div><div class="parent-3">
    <p class="childP">bro
  </div></section>
英文:

It is for the whitespace between each of the child &lt;div&gt; elements. Here is the markup with the whitespace removed, and thus no Text nodes are present in .childNodes.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(document.querySelector(&#39;.section1&#39;).childNodes)

<!-- language: lang-html -->

&lt;section class=&quot;section1&quot;&gt;&lt;div class=&quot;parent&quot;&gt;
    &lt;p class=&quot;childP&quot;&gt;hello&lt;/p&gt;
  &lt;/div&gt;&lt;div class=&quot;parent&quot;&gt;
    &lt;p class=&quot;childP&quot;&gt;bro
  &lt;/div&gt;&lt;div class=&quot;parent-3&quot;&gt;
    &lt;p class=&quot;childP&quot;&gt;bro
  &lt;/div&gt;&lt;/section&gt;

<!-- end snippet -->

答案2

得分: 0

childNodes会返回目标元素的直接子节点中的任何elementtextcomment节点。

所以,问题是文本节点是从哪里来的?

div元素之间的换行被视为文本节点。

如果去掉换行,就不会有文本节点。

console.log([...document.querySelector('.section1').childNodes].map(e => e.nodeName))
<section class="section1"><div class="parent"><p class="childP">hello</p></div><div class="parent"><p class="childP">bro</div><div class="parent-3"><p class="childP">bro</div></section>
英文:

childNodes will return any element or text or comment nodes whose direct children of the target element.

So, the question is where do the text nodes come from?!

The new lines between the divs element is considered as a text node.

If, you remove the new lines, there will be no text nodes.
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log([...document.querySelector(&#39;.section1&#39;).childNodes].map(e =&gt; e.nodeName))

<!-- language: lang-html -->

&lt;section class=&quot;section1&quot;&gt;&lt;div class=&quot;parent&quot;&gt;&lt;p class=&quot;childP&quot;&gt;hello&lt;/p&gt;&lt;/div&gt;&lt;div class=&quot;parent&quot;&gt;&lt;p class=&quot;childP&quot;&gt;bro&lt;/div&gt;&lt;div class=&quot;parent-3&quot;&gt;&lt;p class=&quot;childP&quot;&gt;bro&lt;/div&gt;&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 17:36:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定