HTML and CSS Reference
branching, tree-like structure, the DOM describes how the nodes are nested inside of
ponents on the page can be accessed using dot notation to traverse the tree structure. If
you are not familiar with dot notation, it simply means that one node in the DOM tree
that is nested inside another can be accessed through its containing nodes (known as
objects in this context) by supplying the node (object) name separated by a period. For
example, the HTML page is represented in the DOM as an object named document ,
which contains the actual HTML page contents. Since the HTML page contains a head
and a body, there is a head and a body object inside the document object. So, to
document.body . This won't actually do anything; the body element is just being
structure of the page is represented by nested objects, each of which can be accessed
using a dot (period).
Once you get down to the specific HTML elements, the commands get more gen-
eralized, because it is unknown in advance what elements are on your particular page.
inside the body of the HTML page, for dynamically updating content, for responding
to events, and so on. For instance, to access the first HTML element shown on the
page, you would use document.body.firstElementChild . To access the first
attribute of that element, you would use docu-
ment.body.firstElementChild.attributes . The zero in brackets just
refers to the number of the attribute to access; 0 refers to the first of the attributes
in the HTML element, 1 to the second attribute, and so forth. Lastly, to access the
contents of this element, you would use the following: docu-
Figure 1-4 shows the structure of a simple web page with only a link in the body area.
Looking at this diagram, you'll notice an extra object at the top, called window . While
the DOM is accessible through the document object, document is actually contained
in this window object, which represents the web browser window the page content is
contained in. Technically, accessing the DOM would be done through the window ob-
ject first, which is the “root” object through which all other aspects of the web page
with window.document.body or document.body (in which case, a reference to
window at the beginning is implied).