Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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 same
nextSibling, previousSibling
parent
the element that contains this
parentNode
node
complete list of DOM node properties
browser incompatiblity information
CS380
DOM tree traversal example
6
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
cleanWhitespac
absolutize addClassName classNames clonePosition
e
cumulativeOffse cumulativeScroll
empty extend firstDescendant
t Offset
getDimensions getHeight getOffsetParent getStyle getWidth
hasClassName hide identify insert inspect
positionedOffse
makeClipping makePositioned match readAttribute
t
recursivelyColle removeClassNa
relativize remove replace
ct me
scrollTo select setOpacity setStyle show
toggleClassNa
toggle undoClipping undoPositioned update
me
viewportOffset visible wrap writeAttribute
Prototype's DOM tree traversal
9
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
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 objects
11
name description
<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 selecting
14
elements
var gameButtons = $("game").select("button.control");
for (var i = 0; i < gameButtons.length; i++) {
gameButtons[i].style.color = "yellow";
} JS
CS380
Prototype's methods for selecting
<ul id="fruits">
<li id="apples">apples
<ul>
15
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 methods for selecting
<ul id="fruits">
<li id="apples">
<h3 title="yummy!">Apples</h3>
16
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]
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
CS380
Removing a node from the page
21
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 of
25
existing styles
this.style.top = this.getStyle("top") + 100 + "px";
// bad!
JS
CS380
Setting CSS classes in Prototype
26
function highlightField() {
// turn text yellow and make it bigger
if (!$("text").hasClassName("invalid")) {
$("text").addClassName("highlight");
}
}
JS
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://www.thinkgeek.com/
http://despair.com/
http://www.redbubble.com/
http://googleresearch.blogspot.com/
CS380