XML DOM - Element 對(duì)象
Element 對(duì)象表示 XML 文檔中的元素。
Element 對(duì)象
Element 對(duì)象表示 XML 文檔中的元素。元素可包含屬性、其他元素或文本。如果元素含有文本,則在文本節(jié)點(diǎn)中表示該文本。
重要事項(xiàng):文本永遠(yuǎn)存儲(chǔ)在文本節(jié)點(diǎn)中。在 DOM 處理過(guò)程中的一個(gè)常見(jiàn)的錯(cuò)誤是,導(dǎo)航到元素節(jié)點(diǎn),并認(rèn)為此節(jié)點(diǎn)含有文本。不過(guò),即使最簡(jiǎn)單的元素節(jié)點(diǎn)之下也擁有文本節(jié)點(diǎn)。舉例,在 <year>2005</year> 中,有一個(gè)元素節(jié)點(diǎn)(year),同時(shí)此節(jié)點(diǎn)之下存在一個(gè)文本節(jié)點(diǎn),其中含有文本(2005)。
由于元素對(duì)象也是一種節(jié)點(diǎn),因此它可繼承 Node 對(duì)象的屬性和方法。
Element 對(duì)象的屬性
屬性 | 描述 | IE | F | O | W3C |
---|---|---|---|---|---|
attributes | 返回元素的屬性的 NamedNodeMap | 5 | 1 | 9 | Yes |
baseURI | 返回元素的絕對(duì)基準(zhǔn) URI | No | 1 | No | Yes |
childNodes | 返回元素的子節(jié)點(diǎn)的 NodeList | 5 | 1 | 9 | Yes |
firstChild | 返回元素的首個(gè)子節(jié)點(diǎn) | 5 | 1 | 9 | Yes |
lastChild | 返回元素的最后一個(gè)子節(jié)點(diǎn) | 5 | 1 | 9 | Yes |
localName | 返回元素名稱(chēng)的本地部分 | No | 1 | 9 | Yes |
namespaceURI | 返回元素的命名空間 URI | No | 1 | 9 | Yes |
nextSibling | 返回元素之后緊跟的節(jié)點(diǎn) | 5 | 1 | 9 | Yes |
nodeName | 返回節(jié)點(diǎn)的名稱(chēng),依據(jù)其類(lèi)型。 | 5 | 1 | 9 | Yes |
nodeType | 返回節(jié)點(diǎn)的類(lèi)型 | 5 | 1 | 9 | Yes |
ownerDocument | 返回元素所屬的根元素 (document 對(duì)象) | 5 | 1 | 9 | Yes |
parentNode | 返回元素的父節(jié)點(diǎn) | 5 | 1 | 9 | Yes |
prefix | 設(shè)置或返回元素的命名空間前綴 | No | 1 | 9 | Yes |
previousSibling | 返回元素之前緊隨的節(jié)點(diǎn) | 5 | 1 | 9 | Yes |
schemaTypeInfo | 返回與元素相關(guān)聯(lián)的類(lèi)型信息 | No | Yes | ||
tagName | 返回元素的名稱(chēng) | 5 | 1 | 9 | Yes |
textContent | 設(shè)置或返回元素及其后代的文本內(nèi)容 | No | 1 | No | Yes |
text | 返回節(jié)點(diǎn)及其后代的文本 (IE-only) | 5 | No | No | No |
xml | 返回節(jié)點(diǎn)及其后代的 XML (IE-only) | 5 | No | No | No |
Element 對(duì)象的方法
方法 | 描述 | IE | F | O | W3C |
---|---|---|---|---|---|
appendChild() | 向節(jié)點(diǎn)的子節(jié)點(diǎn)列表末尾添加新的子節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
cloneNode() | 克隆節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
compareDocumentPosition() | 比較兩節(jié)點(diǎn)的文檔位置。 | No | 1 | No | Yes |
dispatchEvent() | 給節(jié)點(diǎn)分派一個(gè)合成事件。 | No | 1 | 9 | Yes |
getAttribute() | 返回屬性的值。 | 5 | 1 | 9 | Yes |
getAttributeNS() | 返回屬性的值。 | No | 1 | 9 | Yes |
getAttributeNode() | 以 Attribute 對(duì)象返回屬性節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
getAttributeNodeNS() | 以 Attribute 對(duì)象返回屬性節(jié)點(diǎn)。 | No | 9 | Yes | |
getElementsByTagName() | 找到具有指定標(biāo)簽名的子孫元素。 | 5 | 1 | 9 | Yes |
getElementsByTagNameNS() | 找到具有指定標(biāo)簽名和命名空間的元素。 | No | 1 | 9 | Yes |
getFeature(feature,version) | 返回 DOM 對(duì)象,此對(duì)象可執(zhí)行擁有指定特性和版本的專(zhuān)門(mén)的 API。 | No | Yes | ||
getUserData(key) | 返回關(guān)聯(lián)節(jié)點(diǎn)上鍵的對(duì)象。此對(duì)象必須首先通過(guò)使用相同的鍵來(lái)調(diào)用 setUserData 來(lái)設(shè)置到此節(jié)點(diǎn)。 | No | Yes | ||
hasAttribute() | 返回元素是否擁有指定的屬性。 | 5 | 1 | 9 | Yes |
hasAttributeNS() | 返回元素是否擁有指定的屬性。 | No | 1 | 9 | Yes |
hasAttributes() | 返回元素是否擁有屬性。 | 5 | 1 | 9 | Yes |
hasChildNodes() | 返回元素是否擁有子節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
insertBefore() | 在已有的子節(jié)點(diǎn)之前插入一個(gè)新的子節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
isDefaultNamespace(URI) | 返回指定的命名空間 URI 是否為默認(rèn)。 | No | Yes | ||
isEqualNode() | 檢查兩節(jié)點(diǎn)是否相等。 | No | No | No | Yes |
isSameNode() | 檢查兩節(jié)點(diǎn)是否為同一節(jié)點(diǎn)。 | No | 1 | No | Yes |
isSupported(feature,version) | 返回指定的特性是否在此元素上得到支持。 | 9 | Yes | ||
lookupNamespaceURI() | 返回匹配指定前綴的命名空間 URI。 | No | 1 | No | Yes |
lookupPrefix() | 返回匹配指定的命名空間 URI 的前綴。 | No | 1 | No | Yes |
normalize() | 5 | 1 | 9 | Yes | |
removeAttribute() | 刪除指定的屬性。 | 5 | 1 | 9 | Yes |
removeAttributeNS() | 刪除指定的屬性。 | No | 1 | 9 | Yes |
removeAttributeNode() | 刪除指定的屬性節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
removeChild() | 刪除子節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
replaceChild() | 替換子節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
setUserData(key,data,handler) | 把對(duì)象關(guān)聯(lián)到元素上的鍵。 | No | Yes | ||
setAttribute() | 添加新屬性。 | 5 | 1 | 9 | Yes |
setAttributeNS() | 添加新屬性。 | 1 | 9 | Yes | |
setAttributeNode() | 添加新的屬性節(jié)點(diǎn)。 | 5 | 1 | 9 | Yes |
setAttributeNodeNS(attrnode) | 添加新的屬性節(jié)點(diǎn)。 | 9 | Yes | ||
setIdAttribute(name,isId) | 如果 Attribute 對(duì)象 isId 屬性為 true,那么此方法會(huì)把指定的屬性聲明為一個(gè)用戶(hù)確定 ID 的屬性(user-determined ID attribute)。 | No | Yes | ||
setIdAttributeNS(uri,name,isId) | 如果 Attribute 對(duì)象 isId 屬性為 true,那么此方法會(huì)把指定的屬性聲明為一個(gè)用戶(hù)確定 ID 的屬性(user-determined ID attribute)(帶有命名空間)。 | No | Yes | ||
setIdAttributeNode(idAttr,isId) | 如果 Attribute 對(duì)象 isId 屬性為 true,那么此方法會(huì)把指定的屬性聲明為一個(gè)用戶(hù)確定 ID 的屬性(user-determined ID attribute)。 | No | Yes |