SlideShare a Scribd company logo
1 of 30
The DOM tree
CS380
1
The DOM tree
CS380
2
Types of DOM nodes
 element nodes (HTML tag)
 can have children and/or attributes
 text nodes (text in a block element)
 attribute nodes (attribute/value pair)
 text/attributes are children in an element node
 cannot have children or attributes
 not usually shown when drawing the DOM tree
CS380
3
<p>
This is a paragraph of text with a
<a href="/path/page.html">link in it</a>.
</p> HTML
Types of DOM nodes
CS380
4
<p>
This is a paragraph of text with a
<a href="/path/page.html">link in it</a>.
</p> HTML
Traversing the DOM tree
name(s) description
firstChild, lastChild
start/end of this node's list of
children
childNodes
array of all this node's
children
nextSibling, previousSibling
neighboring nodes with the
same parent
parentNode
the element that contains this
node
CS380
5
•complete list of DOM node properties
•browser incompatiblity information
DOM tree traversal example
6
<p id="foo">This is a paragraph of text with a
<a href="/path/to/another/page.html">link</a>.</p>
HTML
CS380
Elements vs text nodes
 Q: How many children does the div above
have?
 A: 3
 an element node representing the <p>
 two text nodes representing "nt" (before/after the
paragraph)
 Q: How many children does the paragraph
7
<div>
<p>
This is a paragraph of text with a
<a href="page.html">link</a>.
</p>
</div> HTML
Prototype's DOM element
methods
absolutize
addClassNam
e
classNames
cleanWhitesp
ace
clonePosition
cumulativeOff
set
cumulativeScr
ollOffset
empty extend
firstDescenda
nt
getDimension
s
getHeight
getOffsetPare
nt
getStyle getWidth
hasClassNam
e
hide identify insert inspect
makeClipping
makePosition
ed
match
positionedOffs
et
readAttribute
recursivelyCol
lect
relativize remove
removeClass
Name
replace
scrollTo select setOpacity setStyle show
toggle
toggleClassN
ame
undoClipping
undoPositione
d
update
8
Prototype's DOM tree traversal
methods
method(s) description
ancestors, up elements above this one
childElements, descendants,
down
elements below this one (not
text nodes)
siblings, next, nextSiblings,
previous, previousSiblings,
adjacent
elements with same parent
as this one (not text nodes)
CS380
9
Prototype's DOM tree traversal
methods
CS380
10
// 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
Selecting groups of DOM
objects
 methods in document and other DOM objects
for accessing descendants:
CS380
11
name description
getElementsByTagName
returns array of descendents
with the given tag, such as
"div"
getElementsByName
returns array of descendants
with the given name
attribute (mostly useful for
accessing form controls)
Getting all elements of a certain
type
CS380
12
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
CS380
13
var addrParas = $("address").getElementsByTagName("p");
for (var i = 0; i < addrParas.length; i++) {
addrParas[i].style.backgroundColor = "yellow";
} JS
<p>This won't be returned!</p>
<div id="address">
<p>1234 Street</p>
<p>Atlanta, GA</p>
</div> HTML
Prototype's methods for
selecting elements
CS380
14
var gameButtons = $("game").select("button.control");
for (var i = 0; i < gameButtons.length; i++) {
gameButtons[i].style.color = "yellow";
} JS
getElementsByClassName
array of elements that use given
class attribute
select
array of descendants that match
given CSS selector, such as
"div#sidebar ul.news > li"
Prototype adds methods to the document object
(and all DOM element objects) for selecting groups of elements:
Prototype's methods for
selecting elements
CS380
15
<ul id="fruits">
<li id="apples">apples
<ul>
<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
Prototype's methods for
selecting elements
CS380
16
<ul id="fruits">
<li id="apples">
<h3 title="yummy!">Apples</h3>
<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]
$('apples').select( 'p#saying', 'li[title="yummy!"]');
//
$('apples').select('[title="disgusting!"]');
// JS
The $$ function
17
// hide all "announcement" paragraphs in the "news"
//section
var paragraphs = $$("div#news p.announcement");
for (var i = 0; i < paragraphs.length; i++) {
paragraphs[i].hide();
} JS
 $$ returns an array of DOM elements that
match the given CSS selector
 like $ but returns an array instead of a single
DOM object
 a shorthand for document.select
 useful for applying an operation each one of a
set of elements
var arrayName = $$("CSS selector"); JS
Common issues with $$
18
// get all buttons with a class of "control"
var gameButtons = $$("control");
var gameButtons = $$(".control"); JS
// set all buttons with a class of "control" to have red
text
$$(".control").style.color = "red";
var gameButtons = $$(".control");
for (var I = 0; i < gameButtons.length; i++) {
gameButtons[i].style.color = "red";
} JS
Q: Can I still select a group of elements using $$ even if
my CSS file doesn't have any style rule for that same
group? (A: Yes!)
Creating new nodes
19
// create a new <h2> node
var newHeading = document.createElement("h2");
newHeading.innerHTML = "This is a heading";
newHeading.style.color = "green";
JS
 merely creating a node does not add it to the
page
 you must add the new node as a child of an
name description
document.createElement("tag")
creates and returns a new empty
DOM node representing an
element of that type
document.createTextNode("text")
creates and returns a text node
containing given text
Modifying the DOM tree
20
var p = document.createElement("p");
p.innerHTML = "A paragraph!";
$("main").appendChild(p);
JS
name description
appendChild(node)
places given node at end of this
node's child list
insertBefore(new, old)
places the given new node in this
node's child list just before old
child
removeChild(node)
removes given node from this
node's child list
replaceChild(new, old)
replaces given child with new
node
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
 each DOM object has a removeChild method
to remove its children from the page
 Prototype adds a remove method for a node to
remove itself
CS380
DOM versus innerHTML
hacking
22
function slideClick() {
$("thisslide").innerHTML += "<p>A paragraph!</p>";
} JS
 Imagine that the new node is more complex:
 ugly: bad style on many levels (e.g. JS code
embedded within HTML)
 error-prone: must carefully distinguish " and '
 can only add at beginning or end, not in middle of
child list
Why not just code the previous example this
way?
function slideClick() {
this.innerHTML += "<p style='color: red; " +
"margin-left: 50px;' " +
"onclick='myOnClick();'>" +
"A paragraph!</p>";
} JS
Problems with reading/changing
styles
23
window.onload = function() {
$("clickme").onclick = biggerFont;
};
function biggerFont() {
var size = parseInt($("clickme").style.fontSize);
size += 4;
$("clickMe").style.fontSize = size + "pt";
} JS
 style property lets you set any CSS style for an
element
 problem: you cannot (usually) read existing
styles with it
CS380
Accessing styles in Prototype
24
function biggerFont() {
// turn text yellow and make it bigger
var size = parseInt($("clickme").getStyle("font-
size"));
$("clickme").style.fontSize = (size + 4) + "pt";
} JS
 getStyle function added to DOM object allows
accessing existing styles
 addClassName, removeClassName,
hasClassName manipulate CSS classes
CS380
Common bug: incorrect usage
of existing styles
25
this.style.top = this.getStyle("top") + 100 + "px";
// bad!
JS
 the above example computes e.g. "200px" +
100 + "px" , which would evaluate to
"200px100px"
 a corrected version:
CS380
this.style.top = parseInt(this.getStyle("top")) + 100 +
"px"; // correct
JS
Setting CSS classes in
Prototype
26
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
CS380
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>
Example: createElements
CS380
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
Javascript Exercises
 Create a webpage with an of Homer Simpson
image at the center of the page. Develop a
script that prints an alert: “Duh, you are
hovering!!” every time the mouse crosses over
the image.
 Add 5 buttons to your webpage: red, yellow,
green, black, and silver. Every time you click
on one of these buttons the background
should take the corresponding color.
CS380
29
Javascript Exercises
 Add a link with the text: “CLICK ME!”. Develop
a function that randomly chooses between the
following websites to link your text:
 http://slashdot.org/
 http://www.thinkgeek.com/
 http://despair.com/
 http://www.redbubble.com/
 http://googleresearch.blogspot.com/
CS380
30

More Related Content

Similar to DOM TREE VISUALIZATION

Polymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web componentsPolymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web componentspsstoev
 
Vaadin Components @ Angular U
Vaadin Components @ Angular UVaadin Components @ Angular U
Vaadin Components @ Angular UJoonas Lehtinen
 
Introduction to HTML-CSS-Javascript.pdf
Introduction to HTML-CSS-Javascript.pdfIntroduction to HTML-CSS-Javascript.pdf
Introduction to HTML-CSS-Javascript.pdfDakshPratapSingh1
 
The Ring programming language version 1.6 book - Part 47 of 189
The Ring programming language version 1.6 book - Part 47 of 189The Ring programming language version 1.6 book - Part 47 of 189
The Ring programming language version 1.6 book - Part 47 of 189Mahmoud Samir Fayed
 
14922 java script built (1)
14922 java script built (1)14922 java script built (1)
14922 java script built (1)dineshrana201992
 
CSS- Smacss Design Rule
CSS- Smacss Design RuleCSS- Smacss Design Rule
CSS- Smacss Design Rule皮馬 頑
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivityMouli Chandira
 
The Ring programming language version 1.2 book - Part 33 of 84
The Ring programming language version 1.2 book - Part 33 of 84The Ring programming language version 1.2 book - Part 33 of 84
The Ring programming language version 1.2 book - Part 33 of 84Mahmoud Samir Fayed
 
Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013Sébastien Deleuze
 
ASP.net Manual final.pdf
ASP.net Manual final.pdfASP.net Manual final.pdf
ASP.net Manual final.pdfSwapnilGujar13
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”apostlion
 
asp.net - for merge.docx
asp.net - for merge.docxasp.net - for merge.docx
asp.net - for merge.docxSwapnilGujar13
 
CSS3 and jQuery
CSS3 and jQueryCSS3 and jQuery
CSS3 and jQuerypsophy
 

Similar to DOM TREE VISUALIZATION (20)

Polymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web componentsPolymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web components
 
HTML and CSS part 2
HTML and CSS part 2HTML and CSS part 2
HTML and CSS part 2
 
Vaadin Components @ Angular U
Vaadin Components @ Angular UVaadin Components @ Angular U
Vaadin Components @ Angular U
 
Introduction to HTML-CSS-Javascript.pdf
Introduction to HTML-CSS-Javascript.pdfIntroduction to HTML-CSS-Javascript.pdf
Introduction to HTML-CSS-Javascript.pdf
 
The Ring programming language version 1.6 book - Part 47 of 189
The Ring programming language version 1.6 book - Part 47 of 189The Ring programming language version 1.6 book - Part 47 of 189
The Ring programming language version 1.6 book - Part 47 of 189
 
14922 java script built (1)
14922 java script built (1)14922 java script built (1)
14922 java script built (1)
 
CSS- Smacss Design Rule
CSS- Smacss Design RuleCSS- Smacss Design Rule
CSS- Smacss Design Rule
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
Polymer 1.0
Polymer 1.0Polymer 1.0
Polymer 1.0
 
Html advance
Html advanceHtml advance
Html advance
 
HTML-Advance.pptx
HTML-Advance.pptxHTML-Advance.pptx
HTML-Advance.pptx
 
The Ring programming language version 1.2 book - Part 33 of 84
The Ring programming language version 1.2 book - Part 33 of 84The Ring programming language version 1.2 book - Part 33 of 84
The Ring programming language version 1.2 book - Part 33 of 84
 
Zend framework 04 - forms
Zend framework 04 - formsZend framework 04 - forms
Zend framework 04 - forms
 
Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013
 
ASP.net Manual final.pdf
ASP.net Manual final.pdfASP.net Manual final.pdf
ASP.net Manual final.pdf
 
CSS3 vs jQuery
CSS3 vs jQueryCSS3 vs jQuery
CSS3 vs jQuery
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
jQuery Beginner
jQuery BeginnerjQuery Beginner
jQuery Beginner
 
asp.net - for merge.docx
asp.net - for merge.docxasp.net - for merge.docx
asp.net - for merge.docx
 
CSS3 and jQuery
CSS3 and jQueryCSS3 and jQuery
CSS3 and jQuery
 

Recently uploaded

VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Roomdivyansh0kumar0
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...aditipandeya
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607dollysharma2066
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts servicesonalikaur4
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一Fs
 
AlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsAlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsThierry TROUIN ☁
 
Complet Documnetation for Smart Assistant Application for Disabled Person
Complet Documnetation   for Smart Assistant Application for Disabled PersonComplet Documnetation   for Smart Assistant Application for Disabled Person
Complet Documnetation for Smart Assistant Application for Disabled Personfurqan222004
 
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012rehmti665
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kestopur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Roomdivyansh0kumar0
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...akbard9823
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITMgdsc13
 

Recently uploaded (20)

VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
 
AlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsAlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with Flows
 
Complet Documnetation for Smart Assistant Application for Disabled Person
Complet Documnetation   for Smart Assistant Application for Disabled PersonComplet Documnetation   for Smart Assistant Application for Disabled Person
Complet Documnetation for Smart Assistant Application for Disabled Person
 
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kestopur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kestopur 👉 8250192130 Available With Room
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
 
Call Girls Service Dwarka @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
Call Girls Service Dwarka @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICECall Girls Service Dwarka @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICE
Call Girls Service Dwarka @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITM
 

DOM TREE VISUALIZATION

  • 3. Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in a block element)  attribute nodes (attribute/value pair)  text/attributes are children in an element node  cannot have children or attributes  not usually shown when drawing the DOM tree CS380 3 <p> This is a paragraph of text with a <a href="/path/page.html">link in it</a>. </p> HTML
  • 4. Types of DOM nodes CS380 4 <p> This is a paragraph of text with a <a href="/path/page.html">link in it</a>. </p> HTML
  • 5. Traversing the DOM tree name(s) description firstChild, lastChild start/end of this node's list of children childNodes array of all this node's children nextSibling, previousSibling neighboring nodes with the same parent parentNode the element that contains this node CS380 5 •complete list of DOM node properties •browser incompatiblity information
  • 6. DOM tree traversal example 6 <p id="foo">This is a paragraph of text with a <a href="/path/to/another/page.html">link</a>.</p> HTML CS380
  • 7. Elements vs text nodes  Q: How many children does the div above have?  A: 3  an element node representing the <p>  two text nodes representing "nt" (before/after the paragraph)  Q: How many children does the paragraph 7 <div> <p> This is a paragraph of text with a <a href="page.html">link</a>. </p> </div> HTML
  • 8. Prototype's DOM element methods absolutize addClassNam e classNames cleanWhitesp ace clonePosition cumulativeOff set cumulativeScr ollOffset empty extend firstDescenda nt getDimension s getHeight getOffsetPare nt getStyle getWidth hasClassNam e hide identify insert inspect makeClipping makePosition ed match positionedOffs et readAttribute recursivelyCol lect relativize remove removeClass Name replace scrollTo select setOpacity setStyle show toggle toggleClassN ame undoClipping undoPositione d update 8
  • 9. Prototype's DOM tree traversal methods method(s) description ancestors, up elements above this one childElements, descendants, down elements below this one (not text nodes) siblings, next, nextSiblings, previous, previousSiblings, adjacent elements with same parent as this one (not text nodes) CS380 9
  • 10. Prototype's DOM tree traversal methods CS380 10 // 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
  • 11. Selecting groups of DOM objects  methods in document and other DOM objects for accessing descendants: CS380 11 name description getElementsByTagName returns array of descendents with the given tag, such as "div" getElementsByName returns array of descendants with the given name attribute (mostly useful for accessing form controls)
  • 12. Getting all elements of a certain type CS380 12 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
  • 13. Combining with getElementById CS380 13 var addrParas = $("address").getElementsByTagName("p"); for (var i = 0; i < addrParas.length; i++) { addrParas[i].style.backgroundColor = "yellow"; } JS <p>This won't be returned!</p> <div id="address"> <p>1234 Street</p> <p>Atlanta, GA</p> </div> HTML
  • 14. Prototype's methods for selecting elements CS380 14 var gameButtons = $("game").select("button.control"); for (var i = 0; i < gameButtons.length; i++) { gameButtons[i].style.color = "yellow"; } JS getElementsByClassName array of elements that use given class attribute select array of descendants that match given CSS selector, such as "div#sidebar ul.news > li" Prototype adds methods to the document object (and all DOM element objects) for selecting groups of elements:
  • 15. Prototype's methods for selecting elements CS380 15 <ul id="fruits"> <li id="apples">apples <ul> <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
  • 16. Prototype's methods for selecting elements CS380 16 <ul id="fruits"> <li id="apples"> <h3 title="yummy!">Apples</h3> <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] $('apples').select( 'p#saying', 'li[title="yummy!"]'); // $('apples').select('[title="disgusting!"]'); // JS
  • 17. The $$ function 17 // hide all "announcement" paragraphs in the "news" //section var paragraphs = $$("div#news p.announcement"); for (var i = 0; i < paragraphs.length; i++) { paragraphs[i].hide(); } JS  $$ returns an array of DOM elements that match the given CSS selector  like $ but returns an array instead of a single DOM object  a shorthand for document.select  useful for applying an operation each one of a set of elements var arrayName = $$("CSS selector"); JS
  • 18. Common issues with $$ 18 // get all buttons with a class of "control" var gameButtons = $$("control"); var gameButtons = $$(".control"); JS // set all buttons with a class of "control" to have red text $$(".control").style.color = "red"; var gameButtons = $$(".control"); for (var I = 0; i < gameButtons.length; i++) { gameButtons[i].style.color = "red"; } JS Q: Can I still select a group of elements using $$ even if my CSS file doesn't have any style rule for that same group? (A: Yes!)
  • 19. Creating new nodes 19 // create a new <h2> node var newHeading = document.createElement("h2"); newHeading.innerHTML = "This is a heading"; newHeading.style.color = "green"; JS  merely creating a node does not add it to the page  you must add the new node as a child of an name description document.createElement("tag") creates and returns a new empty DOM node representing an element of that type document.createTextNode("text") creates and returns a text node containing given text
  • 20. Modifying the DOM tree 20 var p = document.createElement("p"); p.innerHTML = "A paragraph!"; $("main").appendChild(p); JS name description appendChild(node) places given node at end of this node's child list insertBefore(new, old) places the given new node in this node's child list just before old child removeChild(node) removes given node from this node's child list replaceChild(new, old) replaces given child with new node CS380
  • 21. 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  each DOM object has a removeChild method to remove its children from the page  Prototype adds a remove method for a node to remove itself CS380
  • 22. DOM versus innerHTML hacking 22 function slideClick() { $("thisslide").innerHTML += "<p>A paragraph!</p>"; } JS  Imagine that the new node is more complex:  ugly: bad style on many levels (e.g. JS code embedded within HTML)  error-prone: must carefully distinguish " and '  can only add at beginning or end, not in middle of child list Why not just code the previous example this way? function slideClick() { this.innerHTML += "<p style='color: red; " + "margin-left: 50px;' " + "onclick='myOnClick();'>" + "A paragraph!</p>"; } JS
  • 23. Problems with reading/changing styles 23 window.onload = function() { $("clickme").onclick = biggerFont; }; function biggerFont() { var size = parseInt($("clickme").style.fontSize); size += 4; $("clickMe").style.fontSize = size + "pt"; } JS  style property lets you set any CSS style for an element  problem: you cannot (usually) read existing styles with it CS380
  • 24. Accessing styles in Prototype 24 function biggerFont() { // turn text yellow and make it bigger var size = parseInt($("clickme").getStyle("font- size")); $("clickme").style.fontSize = (size + 4) + "pt"; } JS  getStyle function added to DOM object allows accessing existing styles  addClassName, removeClassName, hasClassName manipulate CSS classes CS380
  • 25. Common bug: incorrect usage of existing styles 25 this.style.top = this.getStyle("top") + 100 + "px"; // bad! JS  the above example computes e.g. "200px" + 100 + "px" , which would evaluate to "200px100px"  a corrected version: CS380 this.style.top = parseInt(this.getStyle("top")) + 100 + "px"; // correct JS
  • 26. Setting CSS classes in Prototype 26 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
  • 27. Example: createElements CS380 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>
  • 28. Example: createElements CS380 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
  • 29. Javascript Exercises  Create a webpage with an of Homer Simpson image at the center of the page. Develop a script that prints an alert: “Duh, you are hovering!!” every time the mouse crosses over the image.  Add 5 buttons to your webpage: red, yellow, green, black, and silver. Every time you click on one of these buttons the background should take the corresponding color. CS380 29
  • 30. Javascript Exercises  Add a link with the text: “CLICK ME!”. Develop a function that randomly chooses between the following websites to link your text:  http://slashdot.org/  http://www.thinkgeek.com/  http://despair.com/  http://www.redbubble.com/  http://googleresearch.blogspot.com/ CS380 30

Editor's Notes

  1. The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods for traversing this tree
  2. categories: CSS classes, DOM tree traversal/manipulation, events, styles
  3. Prototype strips out the unwanted text nodes notice that these are methods, so you need ()
  4. Prototype strips out the unwanted text nodes notice that these are methods, so you need ()
  5. -> [li#golden-delicious, li#mutsu, p#saying]