XML DOM - Element объект

The Element object represents an element in an XML document. Elements can contain attributes, other elements, or text. If an element contains text, it is represented by the text node.

Element object

The Element object represents an element in an XML document. Elements can contain attributes, other elements, or text. If an element contains text, it is represented by the text node.

Important note:Text is always stored in text nodes. A common mistake in DOM processing is to navigate to an element node and assume that this node contains text. However, even the simplest element node has a text node below it. For example, in <year>2005</year>, there is an element node (year), and there is also a text node below this node, which contains the text (2005).

Since the element object is also a node, it can inherit properties and methods from the Node object.

Properties of Element object

Properties Description IE F O W3C
attributes Return the NamedNodeMap of the element's attributes 5 1 9 Да
baseURI Return the absolute base URI of the element Нет 1 Нет Да
childNodes Return the NodeList of child nodes of the element 5 1 9 Да
firstChild Return the first child node of the element 5 1 9 Да
lastChild Return the last child node of the element 5 1 9 Да
localName Return the local part of the element name Нет 1 9 Да
namespaceURI Return the namespace URI of the element Нет 1 9 Да
nextSibling Return the node that follows the element 5 1 9 Да
nodeName Return the name of the node, based on its type. 5 1 9 Да
nodeType Return the type of the node 5 1 9 Да
ownerDocument Return the root element of the element (document object) 5 1 9 Да
parentNode Return the parent node of the element 5 1 9 Да
prefix Set or return the namespace prefix of the element Нет 1 9 Да
previousSibling Return the node that precedes the element 5 1 9 Да
schemaTypeInfo Return type information associated with the element     Нет Да
tagName Return the name of the element 5 1 9 Да
textContent Set or return the text content of the element and its descendants Нет 1 Нет Да
text Return text of the node and its descendants (IE-only) 5 Нет Нет Нет
xml Return XML of the node and its descendants (IE-only) 5 Нет Нет Нет

Methods of Element object

Method Description IE F O W3C
appendChild() Add a new child node to the end of the node's child node list. 5 1 9 Да
cloneNode() Clone a node. 5 1 9 Да
compareDocumentPosition() Compare the document positions of two nodes. Нет 1 Нет Да
dispatchEvent() Assign a composite event to the node. Нет 1 9 Да
getAttribute() 返回属性的值。 5 1 9 Да
getAttributeNS() 返回属性的值。 Нет 1 9 Да
getAttributeNode() 以 Attribute 对象返回属性节点。 5 1 9 Да
getAttributeNodeNS() 以 Attribute 对象返回属性节点。 Нет   9 Да
getElementsByTagName() 找到具有指定标签名的子孙元素。 5 1 9 Да
getElementsByTagNameNS() 找到具有指定标签名和命名空间的元素。 Нет 1 9 Да
getFeature(feature,version) 返回 DOM 对象,此对象可执行拥有指定特性和版本的专门的 API。     Нет Да
getUserData(key) 返回关联节点上键的对象。此对象必须首先通过使用相同的键来调用 setUserData 来设置到此节点。     Нет Да
hasAttribute() 返回元素是否拥有指定的属性。 5 1 9 Да
hasAttributeNS() 返回元素是否拥有指定的属性。 Нет 1 9 Да
hasAttributes() 返回元素是否拥有属性。 5 1 9 Да
hasChildNodes() 返回元素是否拥有子节点。 5 1 9 Да
insertBefore() 在已有的子节点之前插入一个新的子节点。 5 1 9 Да
isDefaultNamespace(URI) 返回指定的命名空间 URI 是否为默认。     Нет Да
isEqualNode() 检查两节点是否相等。 Нет Нет Нет Да
isSameNode() 检查两节点是否为同一节点。 Нет 1 Нет Да
isSupported(feature,version) 返回指定的特性是否在此元素上得到支持。     9 Да
lookupNamespaceURI() 返回匹配指定前缀的命名空间 URI。 Нет 1 Нет Да
lookupPrefix() 返回匹配指定的命名空间 URI 的前缀。 Нет 1 Нет Да
normalize()   5 1 9 Да
removeAttribute() 删除指定的属性。 5 1 9 Да
removeAttributeNS() 删除指定的属性。 Нет 1 9 Да
removeAttributeNode() 删除指定的属性节点。 5 1 9 Да
removeChild() 删除子节点。 5 1 9 Да
replaceChild() 替换子节点。 5 1 9 Да
setUserData(key,data,handler) 将对象关联到元素上的键。     Нет Да
setAttribute() 添加新属性。 5 1 9 Да
setAttributeNS() 添加新属性。   1 9 Да
setAttributeNode() Добавление нового узла атрибута. 5 1 9 Да
setAttributeNodeNS(attrnode) Добавление нового узла атрибута.     9 Да
setIdAttribute(name,isId) Если атрибут isId объекта Attribute равен true, то этот метод объявляет指定的 атрибут свойством пользователя确定的 ID (user-determined ID attribute).     Нет Да
setIdAttributeNS(uri,name,isId) Если атрибут isId объекта Attribute равен true, то этот метод объявляет指定的 атрибут свойством пользователя确定的 ID (user-determined ID attribute) (с命名ным пространством).     Нет Да
setIdAttributeNode(idAttr,isId) Если атрибут isId объекта Attribute равен true, то этот метод объявляет指定的 атрибут свойством пользователя确定的 ID (user-determined ID attribute).     Нет Да