英文:
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 -->
<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>
<!-- end snippet -->
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 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 <div>
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('.section1').childNodes)
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案2
得分: 0
childNodes
会返回目标元素的直接子节点中的任何element
、text
或comment
节点。
所以,问题是文本节点是从哪里来的?
在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 div
s 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('.section1').childNodes].map(e => e.nodeName))
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论