DOM 基礎 (1)

DOM 是語言無關的 API.談論 DOM 樹的時候指的是節點的層次

[list]
[*]Document - 最頂層節點,其他節點附屬於它

[*]DocumentType - DTD 引用節點

[*]DocumentFragment - 可以像 Document 一樣保存其他節點

[*]Element - 表示起始標籤和結束標籤之間的內容

[*]Attr - 表示屬性名和 value

[*]Text - 表示標籤之間的文本,或者 CDATA section 包含的文本

[*]CDataSection - 節點

[*]Entity - Entity 定義節點,<!ENTITY foo "foo">

[*]EntityReference - 實體引用節點 , "

[*]ProcessingInstruction - PI 節點

[*]Comment - 註釋節點

[*]Notation - 表示 DTD 中定義的記號

[/list]

一個 example:


<?xml version="1.0">
<employees>
<!-- only employee -->
<employee>
<name>Tom</name>
<position>Programmer</position>
<comments><![CDATA[his birthday is 8/23/1998]]></comments>
</employee>
<employees>


[img]http://photo1.bababian.com/upload14/20081120/8DB8AA68D2A02DA1E4B29C9A69262108_500.jpg" alt="[/img]

Node 接口定義了12 個常量

[list]
[*]Node.ELEMENT_NODE(1)

[*]Node.ATTRIBUTE_NODE(2)

[*]Node.TEXT_NODE(3)

[*]Node.CDATA_SECTION_NODE(4)

[*]Node.ENTITY_REFERENCE_NODE(5)

[*]Node.ENTITY_NODE(6)

[*]Node.PROCESSING_INSTRUCTION_NODE(7)

[*]Node.COMMENT_NODE(8)

[*]Node.DOCUMENT_NODE(9)

[*]Node.DOCUMENT_TYPE_NODE(10)

[*]Node.DOCUMENT_FRAGMENT_NODE(11)

[*]Node.NOTATION_NODE(12)
[/list]

一些屬性和方法

[b]屬性/方法[/b] [b]返回類型[/b] [b]說明[/b]
nodeName String 節點名字
nodeVale String
nodeType Number
ownerDocument Document 指向節點所屬 Document
firstChild Node childNodes 中第一個
lastChild Node childNodes 中最後一個
childNodes NodeList 子節點列表
previousSibling Node 前一個兄弟,如果該節點是第一個返回null
nextSibling Node 後一個兄弟,如果該節點是最後一個返回null
hasChildNodes Boolean 是否包含子節點
attributes NamedNodeMap
appendChild(node) Node 添加Node 到 childNodes 末尾
removeChild(node) Node 從 childNodes 中刪除 node
replaceChild(newnode, oldnode) Node 替換
insertBefore(newnode, refnode) Node 插入到 refnode 之前


[list]
[*]NodeList - 節點數組,按數值索引
[*]NamedNodeMap - 同時用數值名字索引的節點表
[/list]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章