L3 DOMTree
L3 DOMTree
Introduction
2
CS380
Types of DOM nodes
8
<p>
This is a paragraph of text with a
<a href="/path/page.html">link in it</a>.
</p> HTML
<p>
This is a paragraph of text with a
<a href="/path/page.html">link in it</a>.
</p> HTML
DOM HTML
10
CS380
Javascript for HTML DOM
11
With the object model, JavaScript gets all the power it needs
to create dynamic HTML:
JavaScript can change all the HTML elements in the page
JavaScript can change all the HTML attributes in the page
JavaScript can change all the CSS styles in the page
JavaScript can remove existing HTML elements and attributes
JavaScript can add new HTML elements and attributes
JavaScript can react to all existing HTML events in the page
JavaScript can create new HTML events in the page
Refer to https://www.w3schools.com/js/js_htmldom.asp
Example
12
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
CS380
Traversing the DOM tree
16
name(s) description
start/end of this node's list of
firstChild, lastChild
children
childNodes array of all this node's children
neighboring nodes with the
nextSibling, previousSibling
same parent
the element that contains this
parentNode
node
DOM tree traversal example
17
<div>
<p>
This is a paragraph of text with a
<a href="page.html">link</a>.
</p>
</div> HTML
Q: How many children does the div above have?
A: 3
an element node representing the <p>
two text nodes representing "\n\t" (before/after the
paragraph)
Q: How many children does the paragraph have?
The a tag?
Prototype's DOM tree traversal
19
methods
method(s) description
ancestors, up elements above this one
childElements, descendants, elements below this one (not
down text nodes)
siblings, next, nextSiblings,
elements with same parent
previous, previousSiblings,
as this one (not text nodes)
adjacent
DOM tree traversal methods
20
Selecting groups of DOM objects
21
name description
returns array of descendents
getElementsByTagName with the given tag, such as
"div"
returns array of descendants
with the given name attribute
getElementsByName
(mostly useful for accessing
form controls)
Getting all elements of a certain
22
type
var allParas = document.getElementsByTagName("p");
for (var i = 0; i < allParas.length; i++) {
allParas[i].style.backgroundColor = "yellow";
} JS
<body>
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>You get the idea...</p>
</body> HTML
Combining with getElementById
23
name description
creates and returns a new empty
document.createElement("tag") DOM node representing an element
of that type
creates and returns a text node
document.createTextNode("text")
containing given text
name description
places given node at end of this
appendChild(node)
node's child list
places the given new node in this
insertBefore(new, old)
node's child list just before old child
removes given node from this node's
removeChild(node)
child list
replaceChild(new, old) replaces given child with new node
var p = document.createElement("p");
p.innerHTML = "A paragraph!";
$("main").appendChild(p);
JS
Removing a node from the page
26
function slideClick() {
var bullets = document.getElementsByTagName("li");
for (var i = 0; i < bullets.length; i++) {
if (bullets[i].innerHTML.indexOf("children") >= 0)
{
bullets[i].remove();
}
}
} JS
function biggerFont() {
// turn text yellow and make it bigger
var size = parseInt($("clickme").getStyle("font-
size"));
$("clickme").style.fontSize = (size + 4) + "pt";
} JS
function highlightField() {
// turn text yellow and make it bigger
if (!$("text").hasClassName("invalid")) {
$("text").addClassName("highlight");
}
}
JS