Chapter 5-1
Chapter 5-1
<html>
<head>
<title>DOM Tutorial</title>
</head>
<body>
<h1>DOM Lesson one</h1>
<p>Hello world!</p>
</body>
</html>
From the HTML above:
The <html> node has no parent node; it is the root node
The parent node of the <head> and <body> nodes is the <html> node
The parent node of the "Hello world!" text node is the <p> node
Continue….
And
The <html> node has two child nodes; <head> and <body>
The <head> node has one child node; the <title> node
The <title> node also has one child node; the text node "DOM
Tutorial“
The <h1> and <p> nodes are siblings, and both child nodes of
<body>
The “DOM Lesson one” is the child of <h1> node and “Hello
world” is the child node of <p> node
1.3 DOM Properties and Methods
Some DOM properties:
x.innerHTML - the text value of x
x.nodeName - the name of x
x.nodeValue - the value of x
x.parentNode - the parent node of x and many more
continued…
x.childNodes - the array of child nodes of x
x. firstChild - the first direct child of the current node
x.lastChild - the last child of the current node
Some DOM methods:
document.getElementById(id) – get the element with a specified id
document.getElementsByTagName(name) – get all elements with a
specified tag name
document.createElement() – creates new HTML tag
document.createTextNode() – creates new text content
x.appendChild(node) – insert a child node to x
x.removeChild(node) – remove a child node from x
1.4 DOM Node Access
With the DOM, you can access every node in an HTML document. You
can access a node in three ways:
By using the getElementById() method
By using the getElementsByTagName() method
By navigating the node tree, using the node relationships
Practice
1. Can you write a DOM program that can change the content of the
given <p> tag?
2. Write a DOM program that returns the title of the document
3. Write a program that adds a <p> child to HTML <body>.
1.5 DOM Events
Every element on a web page has certain events which can trigger
JavaScript functions.
We define the events in the HTML elements.
Examples of events:
A mouse click
A web page or an image loading
Mousing over a hot spot on the web page
Selecting an input box in an HTML form
Submitting an HTML form
A keystroke
Events are normally used in combination with functions, and the
function will not be executed before the event occurs.
2. Dynamic HTML(DHTML)
2.1 Introduction to DHTML
DHTML stands for Dynamic HTML.
It is the art of combining HTML, JavaScript, DOM, and CSS.
DHTML is not a scripting language like JavaScript.
It is a feature that allows your browser to be dynamic.
Web pages built with Dynamic HTML are richer and more interactive,
react faster, and don’t use much bandwidth.
The major elements of DHTML include HTML code, scripting
languages such as JavaScript, the Document Object Model, Cascading
Style Sheets, multimedia filters, and the browser itself.
2.2 DHTML JavaScript
A JavaScript can also be used to change the content or attributes of HTML
elements. To change the content of an HTML element:
document.getElementById(id).innerHTML = new HTML
To change the attribute of an HTML element:
document.getElementById(id).attribute = new value
A JavaScript can also change the style of HTML elements. To change the style
of an HTML element:
document.getElementById(id).style.property = new style
2.3 DHTML DOM
The HTML DOM defines the objects and properties of all HTML elements,
and the methods to access them.
In other words, the HTML DOM is a standard for how to get, change, add, or
delete HTML elements.
Using HTML DOM, it is possible to change content of HTML elements.
continued…