16 DOMTree
16 DOMTree
CS380
The DOM tree
2
CS380
Types of DOM nodes
3
<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
CS380
Traversing the DOM tree
5
CS380
DOM tree traversal example
6
HTML
CS380
Elements vs text nodes
7
<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
Prototype's DOM element
8
methods
Prototype's DOM tree traversal
9
methods
CS380
Prototype's DOM tree traversal
10
methods
// alter siblings of "main" that do not contain "Sun"
var sibs = $("main").siblings();
for (var i = 0; i < sibs.length; i++) {
if (sibs[i].innerHTML.indexOf("Sun") < 0) {
sibs[i].innerHTML += " Sunshine";
}
} JS
CS380
Selecting groups of DOM
11
objects
methods in document and other DOM objects
for accessing descendants:
CS380
Getting all elements of a certain
12
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
CS380
Combining with getElementById
13
CS380
Prototype's methods for
14
selecting elements
var gameButtons = $("game").select("button.control");
for (var i = 0; i < gameButtons.length; i++) {
gameButtons[i].style.color = "yellow";
} JS
CS380
Prototype's
<ul id="fruits">
methods for
<li id="apples">apples
<ul>
15
selecting elements
<li id="golden-delicious">Golden Delicious</li>
<li id="mutsu" class="yummy">Mutsu</li>
<li id="mcintosh" class="yummy">McIntosh</li>
<li id="ida-red">Ida Red</li>
</ul>
</li>
<li id="exotic" class="yummy">exotic fruits
<ul>
<li id="kiwi">kiwi</li>
<li id="granadilla">granadilla</li>
</ul>
</li>
</ul>HTML
$('fruits').getElementsByClassName('yummy');
// -> [li#mutsu, …]
$('exotic').getElementsByClassName('yummy');
// -> JS
CS380
Prototype's
<ul id="fruits">
<li id="apples"> methods for
<h3 title="yummy!">Apples</h3>
16
selecting elements
<ul id="list-of-apples">
<li id="golden-delicious" title="yummy!" >Golden
Delicious</li>
<li id="mutsu" title="yummy!">Mutsu</li>
<li id="mcintosh">McIntosh</li>
<li id="ida-red">Ida Red</li>
</ul>
<p id="saying">An apple a day keeps the doctor
away.</p>
</li>
</ul>
$('apples').select('[title="yummy!"]');
// -> [h3, li#golden-delicious, li#mutsu]
var p = document.createElement("p");
p.innerHTML = "A paragraph!";
$("main").appendChild(p);
JS
CS380
Removing a node from the
21
page
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
CS380
Common bug: incorrect usage
25
of existing styles
CS380
Setting CSS classes in
26
Prototype
function highlightField() {
// turn text yellow and make it bigger
if (!$("text").hasClassName("invalid")) {
$("text").addClassName("highlight");
}
}
JS
addClassName, removeClassName,
hasClassName manipulate CSS classes
similar to existing className DOM property, but
don't have to manually split by spaces
CS380
Example: createElements
27
<html>
<head>
<script src="
https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/pr
ototype.js " type="text/javascript"></script>
<script src="paragraph.js "
type="text/javascript"></script>
</head>
<body>
<div id="paragrapharea">
<button id="add">Add a paragraph</button>
</div>
</body>
</html>
CS380
Example: createElements
28
window.onload = function(){
var button = $("add");
button.onclick = addParagraphClick;
}
function addParagraphClick(){
var paragraph = document.createElement("p");
paragraph.innerHTML = "All work and no play makes
Jack a dull boy";
var area = $("paragrapharea");
area.appendChild(paragraph);
}
function addListClick(){
} JS
CS380
Javascript Exercises
29
CS380
Javascript Exercises
30
http://despair.com/
http://www.redbubble.com/
http://googleresearch.blogspot.com/
CS380