Lecture 9 DOM
Lecture 9 DOM
Overview
2
DOM Introduction
3
DOM Introduction
4
DOM Introduction
Import
JavaScript
code
5
DOM Introduction
6
DOM Introduction
Calls to JavaScript
show() function when
mouse moves over/away
from image
7
DOM Introduction
8
DOM Introduction
9
DOM Introduction
10
DOM Introduction
Returns instance
of Element
(DOM-defined
host object)
representing
HTML element
with given id
11
DOM Introduction
Some properties of
Element instance
correspond
to attributes of
HTML element
12
DOM Introduction
13
DOM Introduction
14
DOM Introduction
15
DOM History and Levels
16
Intrinsic Event Handling
17
Intrinsic Event Handling
18
Intrinsic Event Handling
19
Intrinsic Event Handling
20
Intrinsic Event Handling
21
Modifying Element Style
Change
background color
of element
containing cursor
22
Modifying Element Style
23
Modifying Element Style
Like rollover, style needs to be modified
both when entering and exiting the element.
24
Modifying Element Style
Reference to Element instance
representing the td element
25
Modifying Element Style
26
Modifying Element Style
Reference to Element instance
27
Modifying Element Style
28
Modifying Element Style
29
Modifying Element Style
30
Modifying Element Style
31
Modifying Element Style
32
Modifying Element Style
33
Modifying Element Style
34
Modifying Element Style
35
Modifying Element Style
36
Document Tree
37
Document Tree: Node
38
Document Tree: Node
39
Document Tree: Node
40
Document Tree: Node
41
Document Tree: Node
Example HTML document
42
Document Tree: Node
43
Document Tree: Node
44
Document Tree: Node
45
Document Tree: Node
46
Document Tree: Node
Ignore non-Element’s
47
Document Tree: Node
48
Document Tree: Node
Recurse on
child nodes
49
Document Tree: Node
50
Document Tree: Node
51
Document Tree: Node
52
Document Tree: Modification
Initial rendering After user clicks first list item
53
Document Tree: Modification
Find the
li Element
following the
selected one
(if it exists)
54
Document Tree: Modification
Returns null if
no next sibling
55
Document Tree: Modification
Converting
null to Boolean
produces false
56
Document Tree: Modification
Swap nodes
if an li
element
follows
57
Document Tree: Modification
58
Document Tree: Modification
59
Document Tree: document
60
Document Tree: document
61
Document Tree: document
62
Document Tree: Element Nodes
63
Document Tree: Text Nodes
64
Document Tree: Adding Nodes
<body onload="makeCollapsible('collapse1');">
<ol id="collapse1">
<li>First element of ordered list.</li>
<li>Second element.</li>
<li>Third element.</li>
</ol>
<p>
Paragraph following the list (does not collapse).
</p>
</body>
Body of original HTML document:
65
Document Tree: Adding Nodes
<body onload="makeCollapsible('collapse1');">
Added
to DOM
tree:
<ol id="collapse1">
<li>First element of ordered list.</li>
<li>Second element.</li>
<li>Third element.</li>
</ol>
<p>
Paragraph following the list (does not collapse).
</p>
</body> Effect of executing makeCollapsible():
66
Document Tree: Adding Nodes
Added element
is displayed as if
it was part of
the HTML source
document
67
Document Tree: Adding Nodes
68
Document Tree: Adding Nodes
Node
creation
69
Document Tree: Adding Nodes
Node
addition to DOM
tree (rec. doing
this immediately
after creation).
70
Document Tree: Adding Nodes
Attribute
addition
71
Document Tree: Adding Nodes
72
Document Tree: Adding Nodes
73
Document Tree: Adding Nodes
Modifying text.
74
Document Tree: HTML Properties
75
DOM Event Handling
76
DOM Event Handling
77
DOM Event Handling
78
DOM Event Handling
Event
target
79
DOM Event Handling
Event type
80
DOM Event Handling
81
DOM Event Handling
Event handler
Definition
of event
handler
82
DOM Event Handling
Event instance
83
DOM Event Handling
Normally false
(more later)
84
DOM Event Handling
85
DOM Event Handling:
Mouse Events
DOM2 mouse events
click
mousedown
mouseup
mousemove
mouseover
mouseout
Event instances have additional properties for
mouse events
86
DOM Event Handling:
Mouse Events
87
DOM Event Handling:
Mouse Events
Example: mouse “trail”
88
DOM Event Handling:
Mouse Events
HTML document:
Create
“blips” String uniquely
identifying this div
Add event
listener
89
DOM Event Handling:
Mouse Events
Style sheet for “blips”:
90
DOM Event Handling:
Mouse Events
Event handler updateDivs():
91
DOM Event Handling:
Mouse Events
Event handler updateDivs():
92
DOM Event Propagation
93
DOM Event Propagation
94
DOM Event Propagation
95
DOM Event Propagation
96
DOM Event Propagation
97
DOM Event Propagation
98
DOM Event Propagation
body
ol
li
a
2. Target event handlers
99
DOM Event Propagation
100
DOM Event Propagation
101
DOM Event Propagation
3: bubbling
102
DOM Event Propagation
103
Example: Drop-down Menus
When cursor
moves over
upper menu
…
104
Example: Drop-down Menus
…
a drop-down
appears
105
Example: Drop-down Menus
106
Example: Drop-down Menus
Drop-down
disappears
when cursor
leaves both
drop-down
and menu
107
Example: Drop-down Menus
Document structure:
108
Example: Drop-down Menus
109
Example: Drop-down Menus
Document structure:
Top menu
is a table
110
Example: Drop-down Menus
Document structure:
CSS:
Each top
menu item is
a (positioned)
div
111
Example: Drop-down Menus
Document structure:
CSS:
Associated
drop-down is
in a div
that is
out of the normal
flow and initially
invisible
112
Example: Drop-down Menus
Document structure:
Associated
drop-down is
a table
113
Example: Drop-down Menus
114
Example: Drop-down Menus
115
Example: Drop-down Menus
116
Example: Drop-down Menus
117
Example: Drop-down Menus
JavaScript addEventHandlers():
Target
event
handlers
118
Example: Drop-down Menus
JavaScript addEventListener():
119
Example: Drop-down Menus
120
Example: Drop-down Menus
Basic
processing:
change
visibility of
drop-down
121
Example: Drop-down Menus
Ignore
bubbling
mouseover
events from
drop-down
122
Example: Drop-down Menus
Ignore
mouseout
event if
cursor is
remaining
over menu
item or
drop-down
(self or
descendant)
123
Example: Drop-down Menus
124
Example: Drop-down Menus
JavaScript addEventListener():
125
Example: Drop-down Menus
126
Example: Drop-down Menus
Don’t
bother
changing
style if
this event
bubbled
from a
descendant.
127
Example: Drop-down Menus
128
Example: Drop-down Menus
Ignore
mouseout to
a descendant
129
DOM Event Cancellation
130
DOM Form Validation
131
DOM Form Validation
<body onload="addListeners();">
<form id="validatedForm" action="http://www.example.com">
<p>
<label>Required input:
<input type="text"
name="requiredField" id="requiredField" />
</label>
<input type="submit"
name="submit" value="Click to submit" />
</p>
</form>
</body>
132
DOM Form Validation
133
DOM Form Validation
134
DOM Form Validation
135
DOM Form Validation
136
DOM Form Validation
137
DOM Event Generation
138
Detecting Host Objects
139
Detecting Host Objects
140
IE6 and the DOM
141
IE6 and the DOM
142
IE6 and the DOM
IE6:
Value assigned is a function Object (method)
rather than a String.
143
IE6 and the DOM
144
IE6 and the DOM
IE6:
Listener is called as a method
in IE6, so this is a reference
to button
145
IE6 and the DOM
DOM
approach
146
IE6 and the DOM
IE6
approach
147
IE6 and the DOM
DOM
approach
IE6
approach
148
IE6 and the DOM
DOM
approach
IE6
approach
149
IE6 and the DOM
Basically an Array
of call arguments 150
IE6 and the DOM
151
IE6 and the DOM
152
IE6 and the DOM
Use
exception
handling
for convenience
rather than
testing
for existence
of properties
153
IE6 and the DOM
Most type
values
(except
dblclick)
are copied
without
change
154
IE6 and the DOM
IE6 uses
a different
name for
target
155
IE6 and the DOM
157
IE6 and the DOM
158
IE6 and the DOM
Most mouse-event
properties are identical
159
IE6 and the DOM
160
IE6 and the DOM
161
IE6 and the DOM
162
Other Common Host Objects
163
Other Common Host Objects
164
Other Common Host Objects
165
Other Common Host Objects
166
Other Common Host Objects
167
Other Common Host Objects
168
Other Common Host Objects
169
Other Common Host Objects
170
Other Common Host Objects
171
Other Common Host Objects
172
Other Common Host Objects
173
Other Common Host Objects
174
Other Common Host Objects
175
Other Common Host Objects
176
Other Common Host Objects
177