ElementTraversal interface
Opera Presto provides support for the W3C Element Traversal Specification. This interface allows script navigation of the
elements of a DOM tree, excluding all other nodes in the DOM such as text nodes. It also provides attributes to expose the
number of child elements of an element. This set of read-only attributes allows an author to easily navigate between elements
in a document. In conforming implementations of Element Traversal, all objects that implement Element must
also implement the ElementTraversal interface.
Four of the attributes,
firstElementChildlastElementChildpreviousElementSiblingnextElementSiblingeach provide a live reference to another element with the defined relationship to the current element, if the related element exists.
The fifth attribute,
childElementCountexposes the number of child elements of an element, for preprocessing before navigation.
A conforming User Agent must implement all five attributes. A User Agent may implement similar interfaces in other specifications, but such implementation is not required for conformance to this specification, if the User Agent is designed for a minimal code footprint.
Opera Presto's support for the ElementTraversal interface and its attributes is described in the following data table.
| Interface | Attribute | Description | Support |
|---|---|---|---|
ElementTraversal
|
|||
firstElementChild
|
|
Yes | |
lastElementChild
|
|
Yes | |
previousElementSibling
|
|
Yes | |
nextElementSibling
|
|
Yes | |
childElementCount
|
|
Yes |
In a User Agent which implements the ElementTraversal interface, Element has all the properties
and methods of Node and Element as defined in other DOM specifications, and in addition has the
following properties.
| Object | Property | Description | Support |
|---|---|---|---|
Element
|
|||
firstElementChild
|
This read-only property is of type Element.
|
Yes | |
lastElementChild
|
This read-only property is of type Element.
|
Yes | |
previousElementSibling
|
This read-only property is of type Element.
|
Yes | |
nextElementSibling
|
This read-only property is of type Element.
|
Yes | |
childElementCount
|
This read-only property is of type Number.
|
Yes |
previousElementSiblingpreviousElementSibling attributeThe following code example takes an element as a parameter, and returns the element's position in document order within its parent:
function findPosition( el ) {
var pos = 0;
// step through child elements in reverse order
while ( null != el ) {
// navigate to previous sibling
el = el.previousElementSibling;
pos++;
}
return pos;
}
childElementCount
This example demonstrates the utility of the childElementCount attribute. The following code takes an element
as a parameter, and places each of its children equidistantly according to the available space:
function spaceChildren( el ) {
// get count of element nodes
var elCount = el.childElementCount;
var eachWidth = window.innerWidth / (elCount + 1);
// get first child element
var childEl = el.firstElementChild;
// set initial position
var nextPos = eachWidth/2;
// step through child elements one by one
while ( childEl ) {
// position child
childEl.style.setProperty( 'position', 'absolute', '' );
childEl.style.setProperty( 'left', nextPos + 'px', '' );
childEl.style.setProperty( 'width', eachWidth + 'px', '' );
// increment position by width
nextPos += eachWidth;
// then navigate to next child element
childEl = childEl.nextElementSibling;
}
}
Opera Presto supports DOM 2 Range, and supports DOM 2 Traversal with some exceptions.
Opera supports Load and Save, with some exceptions.
Opera supports DOM 3 XPath, with the same exceptions as with XSLT.
Opera supports window.btoa and window.atob for base64 encoding and decoding.
Need help? Hit F1 anytime while using Opera to access our online help files, or go here.