Skip to content

js-dom笔记中的nodetype问题 #17

@Cn-0kr

Description

@Cn-0kr

佬好!
在js的dom笔记中我有一个疑问:

<body>
  <div id="box">
    <p>我是段落A</p>
    <p id="para">我是段落B</p>
    <p>我是段落C</p>
  </div>

  <script>
    var box = document.getElementById('box');
    var para = document.getElementById('para');
    console.log(box.firstChild.nodeType);

这里nodetype的输出是3,但是我并不明白是为什么。于是我去询问了下gpt,得到了答案:
根据W3C标准,nodeType 的值定义如下:

1 - Element 节点
2 - Attribute 节点(已废弃)
3 - Text 节点
4 - CDATASection 节点(仅在 XML 中有效)
5 - EntityReference 节点(已废弃)
6 - Entity 节点(已废弃)
7 - ProcessingInstruction 节点
8 - Comment 节点
9 - Document 节点
10 - DocumentType 节点
11 - DocumentFragment 节点
12 - Notation 节点(已废弃)
第一个节点是多出的空行text,因此返回3.我觉得这里的笔记好像没有讲到nodetype对应的值的问题,因此拉了一个issue,希望您能看到!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions