Document Object Model (DOM) 为文档对象模型,
    它使用对象的表示方式来表示对应的文档结构及其中的内容。

    下面为一个样例 元素在文档中的对象所包含的所有属性。

    1. p#targetaccessKey: ""
    2. align: ""
    3. attributes: Named
    4. NodeMapbaseURI: ""
    5. childElementCount: 0
    6. childNodes: NodeList[1]
    7. children: HTMLCollection[0]
    8. classList: DOMTokenList[0]
    9. className: ""
    10. clientHeight: 0
    11. clientLeft: 0
    12. clientTop: 0
    13. clientWidth: 0
    14. contentEditable: "inherit"
    15. dataset: DOM
    16. StringMapdir: ""
    17. draggable: false
    18. firstChild: text
    19. firstElementChild: null
    20. hidden: false
    21. id: "target"
    22. innerHTML: "Hello, World!"
    23. innerText: "Hello, World!"
    24. isContentEditable: false
    25. lang: ""
    26. lastChild: text
    27. lastElementChild: null
    28. localName: "p"
    29. namespaceURI: "http://www.w3.org/1999/xhtml"
    30. nextElementSibling: null
    31. nextSibling: null
    32. nodeName: "P"
    33. nodeType: 1
    34. nodeValue: null
    35. offsetHeight: 0
    36. offsetLeft: 0
    37. offsetParent: null
    38. offsetTop: 0
    39. offsetWidth: 0
    40. onabort: null
    41. onautocomplete: null
    42. onautocompleteerror: null
    43. onbeforecopy: null
    44. onbeforecut: null
    45. onbeforepaste: null
    46. onblur: null
    47. oncancel: null
    48. oncanplay: null
    49. oncanplaythrough: null
    50. onchange: null
    51. onclose: null
    52. oncontextmenu: null
    53. oncopy: null
    54. oncuechange: null
    55. oncut: null
    56. ondrag: null
    57. ondragend: null
    58. ondragenter: null
    59. ondragleave: null
    60. ondragover: null
    61. ondragstart: null
    62. ondrop: null
    63. ondurationchange: null
    64. onemptied: null
    65. onended: null
    66. onerror: null
    67. onfocus: null
    68. oninput: null
    69. oninvalid: null
    70. onkeydown: null
    71. onkeypress: null
    72. onkeyup: null
    73. onload: null
    74. onloadeddata: null
    75. onloadedmetadata: null
    76. onloadstart: null
    77. onmousedown: null
    78. onmouseenter: null
    79. onmouseleave: null
    80. onmousemove: null
    81. onmouseout: null
    82. onmouseover: null
    83. onmouseup: null
    84. onmousewheel: null
    85. onpaste: null
    86. onpause: null
    87. onplay: null
    88. onplaying: null
    89. onprogress: null
    90. onratechange: null
    91. onreset: null
    92. onresize: null
    93. onscroll: null
    94. onsearch: null
    95. onseeked: null
    96. onseeking: null
    97. onselect: null
    98. onselectstart: null
    99. onshow: null
    100. onstalled: null
    101. onsubmit: null
    102. onsuspend: null
    103. ontimeupdate: null
    104. ontoggle: null
    105. onvolumechange: null
    106. onwaiting: null
    107. onwebkitfullscreenchange: null
    108. onwebkitfullscreenerror: null
    109. outerHTML: "<p id="target">Hello, World!</p>"
    110. outerText: "Hello, World!"
    111. parentElement: null
    112. parentNode: null
    113. prefix: null
    114. previousElementSibling: null
    115. previousSibling: null
    116. scrollHeight: 0
    117. scrollLeft: 0
    118. scrollTop: 0
    119. scrollWidth: 0
    120. shadowRoot: null
    121. spellcheck: true
    122. style: CSSStyle
    123. DeclarationtabIndex: -1
    124. tagName: "P"
    125. textContent: "Hello, World!"
    126. title: ""
    127. translate: true
    128. webkitdropzone: ""
    129. __proto__: HTMLParagraphElement

    通过使用 DOM 提供的 API (Application Program Interface)
    可以动态的修改节点(node),也就是对 DOM 树的直接操作。
    浏览器中通过使用 JavaScript 来实现对于 DOM 树的改动。

    • DOM Core
    • DOM HTML
    • DOM Style
    • DOM Event

    在元素节点中提取自己所需的节点,并予以操作。

    1. // Document.getElementsByTagName()
    2. // 更具标签名找到目标节点的集合,此例中为 <h1>My header</h1>
    3. var node = document.getElementsByTagName('h1')[0];
    4. // Node.parentNode;
    5. // 获得目标节点的父节点,此例中为 body 元素
    6. node.parentNode;
    7. // Node.firstChild
    8. // 获得目标节点的第一个子节点,此例中为 "My header"
    9. node.firstChild;
    10. // Node.lastChild
    11. // 获得目标节点的最后一个子节点,此例中为 "My header"
    12. node.lastChild;
    13. // Node.previousSibling;
    14. // 获得目标节点的前一个相邻节点
    15. node.previousSibling;
    16. // Node.nextSibling;
    17. // 获得目标节点的下一个相邻节点
    18. node.nextSibling;

    常用节点类型

    • ELEMENT_NODE 可使用 Document.createElement('elementName'); 创建
    • TEXT_NODE 可使用 Document.createTextNode('Text Value'); 创建
    • COMMENT_NODE
    • DOCUMENT_TYPE_NODE

    不同节点对应的NodeType类型

    此值可以通过 Node.nodeType 来获取。

    NOTE:此处需要清楚节点元素的区别。我们平常说的元素
    其实指的是节点中得元素节点,所以说节点包含元素,节点还包括文本节点、实体节点等。

    1. // 在选取元素节点后
    2. p.firstElementChild; // <em>Xinyang</em>
    3. p.lastElementChild; // <a href="http://li-xinyang.com">主页</a>