XML DOM - Element objet

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 error 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 containing the text (2005).

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

Properties of the Element object

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

Methods of the 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 Oui
cloneNode() Clone a node. 5 1 9 Oui
compareDocumentPosition() Compare the document positions of two nodes. Non 1 Non Oui
dispatchEvent() Assign a composite event to the node. Non 1 9 Oui
getAttribute() 返回属性的值。 5 1 9 Oui
getAttributeNS() 返回属性的值。 Non 1 9 Oui
getAttributeNode() 以 Attribute 对象返回属性节点。 5 1 9 Oui
getAttributeNodeNS() 以 Attribute 对象返回属性节点。 Non   9 Oui
getElementsByTagName() 找到具有指定标签名的子孙元素。 5 1 9 Oui
getElementsByTagNameNS() 找到具有指定标签名和命名空间的元素。 Non 1 9 Oui
getFeature(feature,version) 返回 DOM 对象,此对象可执行拥有指定特性和版本的专门的 API。     Non Oui
getUserData(key) 返回关联节点上键的对象。此对象必须首先通过使用相同的键来调用 setUserData 来设置到此节点。     Non Oui
hasAttribute() 返回元素是否拥有指定的属性。 5 1 9 Oui
hasAttributeNS() 返回元素是否拥有指定的属性。 Non 1 9 Oui
hasAttributes() 返回元素是否拥有属性。 5 1 9 Oui
hasChildNodes() 返回元素是否拥有子节点。 5 1 9 Oui
insertBefore() 在已有的子节点之前插入一个新的子节点。 5 1 9 Oui
isDefaultNamespace(URI) 返回指定的命名空间 URI 是否为默认。     Non Oui
isEqualNode() 检查两节点是否相等。 Non Non Non Oui
isSameNode() 检查两节点是否为同一节点。 Non 1 Non Oui
isSupported(feature,version) 返回指定的特性是否在此元素上得到支持。     9 Oui
lookupNamespaceURI() 返回匹配指定前缀的命名空间 URI。 Non 1 Non Oui
lookupPrefix() 返回匹配指定的命名空间 URI 的前缀。 Non 1 Non Oui
normalize()   5 1 9 Oui
removeAttribute() 删除指定的属性。 5 1 9 Oui
removeAttributeNS() 删除指定的属性。 Non 1 9 Oui
removeAttributeNode() 删除指定的属性节点。 5 1 9 Oui
removeChild() 删除子节点。 5 1 9 Oui
replaceChild() 替换子节点。 5 1 9 Oui
setUserData(key,data,handler) 把对象关联到元素上的键。     Non Oui
setAttribute() 添加新属性。 5 1 9 Oui
setAttributeNS() 添加新属性。   1 9 Oui
setAttributeNode() Ajouter un nouveau nœud d'attribut. 5 1 9 Oui
setAttributeNodeNS(attrnode) Ajouter un nouveau nœud d'attribut.     9 Oui
setIdAttribute(name,isId) Si l'attribut de l'objet Attribute est isId true, alors cette méthode déclare l'attribut spécifié comme une attribut d'ID déterminé par l'utilisateur (attribut d'ID déterminé par l'utilisateur).     Non Oui
setIdAttributeNS(uri,name,isId) Si l'attribut de l'objet Attribute est isId true, alors cette méthode déclare l'attribut spécifié comme une attribut d'ID déterminé par l'utilisateur (attribut d'ID déterminé par l'utilisateur) (avec espace de noms).     Non Oui
setIdAttributeNode(idAttr,isId) Si l'attribut de l'objet Attribute est isId true, alors cette méthode déclare l'attribut spécifié comme une attribut d'ID déterminé par l'utilisateur (attribut d'ID déterminé par l'utilisateur).     Non Oui