14 - DHTML C12
14 - DHTML C12
DHTML
What is DHTML?
• In this chapter we introduce the Document Object Model (DOM). The
DOM gives you scripting access to all the elements on a web page.
• Inside the browser, the whole web page—paragraphs, forms, tables,
etc.—is represented in an object hierarchy.
• Using JavaScript, you can dynamically create, modify and remove
elements in the page.
DOM
• We introduce the concepts of DOM nodes and DOM trees. We discuss
properties and methods of DOM nodes and cover additional methods
of the document object.
• We show how to dynamically change style properties, which enables
you to create effects, such as user-defined background colors and
animations.
Modeling a Document: DOM Nodes and
Trees
• The document’s getElementById method is the simplest way to access
a specific element in a page. The method returns objects called DOM
nodes.
• Every piece of an HTML5 page (elements, attributes, text, etc.) is
modeled in the web browser by a DOM node. All the nodes in a
document make up the page’s DOM tree, which describes the
relationships among elements. Nodes are related to each other
through child-parent relationships.
• An HTML5 element inside another element is said to be its child—the
containing element is known as the parent.
• A node can have multiple children but only one parent. Nodes with the
same parent node are referred to as siblings.
Viewing a Document’s DOM
• Inspect
• In addition to viewing a document’s DOM structure, the developer
tools in each browser typically enable you to view and modify styles,
view and debug JavaScripts used in the document, view the resources
(such as images) used by the document, and more.
Traversing and Modifying a DOM Tree
• The DOM enables you to programmatically access a document’s
elements, allowing you to modify its contents dynamically using
JavaScript.
• This section introduces some of the DOM-node properties and
methods for traversing the DOM tree, modifying nodes and creating
or deleting content dynamically.
Sample DOM
JS_MAZE101
HTML_MAZE101
Example
DHTML
DOM Collections
The getElementsByTagName() method returns an HTMLCollection
object.
myCollection[1]
DOM Collections
Example
HTML HTMLCollection Length
const myCollection =
document.getElementsByTagName("p");
myCollection[i].style.color = "red";
}
WARNING
An HTMLCollection is NOT an array!
To do so, you have to find the elements first. There are several ways to do this:
Finding HTML elements by id: document.getElementById("intro");
Finding HTML elements by tag name: document.getElementsByTagName("p");
Finding HTML elements by class name: document.getElementsByClassName("intro");
Finding HTML elements by CSS selectors: document.querySelectorAll("p.intro");
Finding HTML elements by HTML object
collections
• document.anchors
• document.body
• document.documentElement
• document.embeds
• document.forms
• document.head
• document.images
Finding HTML elements by HTML object
collections
Example: Find all Links and display them in a div tag
Check the JS