Section 25.89. Element: an HTML or XML element


25.89. Element: an HTML or XML element

DOM Level 1 Core: Node Element

25.89.1. Subinterfaces

HTMLElement

25.89.2. Properties


readonly String tagName

The tag name of the element. This is the string "P" for an HTML <p> element, for example. For HTML documents, the tag name is returned in uppercase, regardless of its capitalization in the document source. XML documents are case-sensitive, and the tag name is returned exactly as it is written in the document source. This property has the same value as the inherited nodeName property of the Node interface.

25.89.3. Methods


addEventListener( )

Adds an event-handler function to the set of event handlers for this element. This is a DOM-standard method supported by all modern browsers except IE.


attachEvent( )

Adds an event-handler function to the set of handlers for this element. This is the IE-specific alternative to addEventListener( ).


detachEvent( )

Removes an event-handler function from this element. This is the IE-specific alternative to the standard removeEventListener( ) method.


dispatchEvent( )

Dispatches a synthetic event to this node.


getAttribute( )

Returns the value of a named attribute as a string.


getAttributeNS( )

Returns the string value of an attribute specified by local name and namespace URI. Useful only with XML documents that use namespaces.


getAttributeNode( )

Returns the value of a named attribute as an Attr node.


getAttributeNodeNS( )

Returns the Attr value of an attribute specified by local name and namespace URI. Useful only with XML documents that use namespaces.


getElementsByTagName( )

Returns an array (technically, a NodeList) of all descendant Element nodes of this element that have the specified tag name, in the order in which they appear in the document.


getElementsByTagNameNS( )

Like getElementsByTagName( ), except that the element tag name is specified by local name and namespace URI. Useful only with XML documents that use namespaces.


hasAttribute( )

Returns true if this element has an attribute with the specified name, or false otherwise. Note that this method returns TRue if the named attribute is explicitly specified in the document source or if the document's DTD specifies a default value for the named attribute.


hasAttributeNS( )

Like hasAttribute( ), except that the attribute is specified by a combination of local name and namespace URI. This method is useful only with XML documents that use namespaces.


removeAttribute( )

Deletes the named attribute from this element. Note, however, that this method deletes only attributes that are explicitly specified in the document source for this element. If the DTD specifies a default value for this attribute, that default becomes the new value of the attribute.


removeAttributeNode( )

Removes the specified Attr node from the list of attributes for this element. Note that this works only to remove attributes that are explicitly specified in the document source for this attribute. If the DTD specifies a default value for the removed attribute, a new Attr node is created to represent the default value of the attribute.


removeAttributeNS( )

Like removeAttribute( ), except that the attribute to be removed is specified by a combination of local name and namespace URI. Useful only for XML documents that use namespaces.


removeEventListener( )

Removes an event-handler function from the set of handlers for this element. This is a standard DOM method implemented by all modern browsers except IE, which uses detachEvent( ).


setAttribute( )

Sets the named attribute to the specified string value. If an attribute with that name does not already exist, a new attribute is added to the element.


setAttributeNode( )

Adds the specified Attr node to the list of attributes for this element. If an attribute with the same name already exists, its value is replaced.


setAttributeNodeNS( )

Like setAttributeNode( ), but this method is suitable for use with nodes returned by Document.createAttributeNS( ). Useful only with XML documents that use namespaces.


setAttributeNS( )

Like setAttribute( ), except that the attribute to be set is specified by the combination of a local name and a namespace URI. Useful only with XML documents that use namespaces.

25.89.4. Description

The Element interface represents HTML or XML elements or tags. The tagName property specifies the name of the element. The documentElement property of a Document refers to the root Element object for that document. The body property of the HTMLDocument object is similar: it refers to the <body> element of the document. To locate a specific named element in an HTML document, use Document.getElementById( ) (and give the element a unique name with the id attribute). To locate elements by tag name, use getElementsByTagName( ), which is a method of both Element and Document. In HTML documents, you can also use similar HTMLDocument.getElementsByName( ) to look up elements based on the value of their name attribute. Finally, you can create new Element objects for insertion into a document with Document.createElement( ).

The addEventListener( ) method (and its IE-specific alternative attachEvent( )) provide a way to register event-handler functions for specific types of events on the element. See Chapter 17 for complete details. Technically, addEventListener( ), removeEventListener( ), and dispatchEvent( ) are defined by the EventTarget interface of the DOM Level 2 Events specification. Since all Element objects implement EventTarget, the methods are listed here instead.

The various other methods of this interface provide access to the attributes of the element. In HTML documents (and many XML documents), all attributes have simple string values, and you can use the simple methods getAttribute( ) and setAttribute( ) for any attribute manipulation you need to do.

If you are working with XML documents that may contain entity references as part of attribute values, you will have to work with Attr objects and their subtree of nodes. You can get and set the Attr object for an attribute with getAttributeNode( ) and setAttributeNode( ), or you can iterate through the Attr nodes in the attributes[] array of the Node interface. If you are working with an XML document that uses XML namespaces, you need to use the various methods whose names end with "NS".

In the DOM Level 1 specification, the normalize( ) method was part of the Element interface. In the Level 2 specification, normalize( ) is instead part of the Node interface. All Element nodes inherit this method and can still use it.

25.89.5. See Also

HTMLElement, Node; Chapter 15, Chapter 17




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net