DOM中的节点类型

时间:2013-10-02 08:51:06

标签: html xml dom

我正在阅读Jeremy Keith撰写的“Dom Scripting”一书。我读到DOM中共有12种类型的节点。我只知道其中五个是 -

Document
Element
Text
Attribute
Comment

我不知道任何其他节点。请告诉我我没有的知识。

2 个答案:

答案 0 :(得分:3)

来自DOM Level 3 specNode界面的IDL:

interface Node {

    // NodeType
    const unsigned short      ELEMENT_NODE                   = 1;
    const unsigned short      ATTRIBUTE_NODE                 = 2;
    const unsigned short      TEXT_NODE                      = 3;
    const unsigned short      CDATA_SECTION_NODE             = 4;
    const unsigned short      ENTITY_REFERENCE_NODE          = 5;
    const unsigned short      ENTITY_NODE                    = 6;
    const unsigned short      PROCESSING_INSTRUCTION_NODE    = 7;
    const unsigned short      COMMENT_NODE                   = 8;
    const unsigned short      DOCUMENT_NODE                  = 9;
    const unsigned short      DOCUMENT_TYPE_NODE             = 10;
    const unsigned short      DOCUMENT_FRAGMENT_NODE         = 11;
    const unsigned short      NOTATION_NODE                  = 12;
    // ...
}

当您对节点有引用并且需要知道它是什么类型(并非所有浏览器都支持常量)时,该数字很有用:

var elem = document.getElementById("example");
console.log(elem.nodeType); // 1

答案 1 :(得分:0)

在浏览器控制台中运行for(key in Node) console.log(key, ' = ' + Node[key])将打印所有受支持的节点类型及其数值。