childNodes-元素内部的空格 [英] childNodes - whitespace inside elements

查看:72
本文介绍了childNodes-元素内部的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div id="myDIV">
<p>First p element (index 1)</p>
<p>Second p element (index 3)</p>
</div>

试图了解索引0为什么是文本节点.

Trying to understand why index 0 is a text node.

根据来自w3schools的示例,索引0、2和& 4个是文本节点.索引1和3是p元素.

According to this example from w3schools, index 0, 2, & 4 are text nodes. Index 1 & 3 are p elements.

索引0是否是文本节点,因为它是空格?如果是这样,此空白在哪里存在?在开始div标签和第一个开始p标签之间?

Is index 0 a text node because it is whitespace? If so, where does this whitespace exist? Between the opening div tag and first opening p tag?

推荐答案

换行符也是空白.如果将这些元素放在一行上,则可以省略空格.

Line breaks are whitespace, too. If you put those elements on a single line, you can omit whitespaces.

<div id="myDIV"><p>First p element (index 0)</p><p>Second p element (index 1)</p></div>

编辑

问题以及我的答案中的索引号都是 node 索引.一个人可以使用他们的EG.使用 childNodes属性.

EDIT

The index numbers in the question, as well as in my answer, are node indices. One can use them EG. using the childNodes property.

另一方面,通过访问

On the other hand, one can use element indices, that do not count text and comment nodes, EG by accessing the childern property.

这篇关于childNodes-元素内部的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆