Dom PDF
Dom PDF
What is DOM
Traversal and Modification
Lecture 4:
Events and Event Handling
Document Object Model
(DOM)
Wendy Liu
CSC309F – Fall 2007
1
2
1
DOM Representation of XML Window
<A> The Window object represents the window that
<B>text</B> displays the document
<C> Properties of the Window object are visible to all
JavaScript scripts that appear in the window’s XHTML
<D>child of C</D> Document
document
<E>another child of C</E> Element The Window object include all of the global variables as
</C> properties
<F>moreText</F> It is the unique global object which is created before
control enters any execution context
</A>
text moreText Text Has a self referential property: window
Document Element
Represent the displayed XHTML document Elements of a document are Element objects
A central interface Can be used to
Create new elements and text nodes Navigate document
Change document tree structure
9 10
2
Example: DOM Tree Modifying DOM
Node
appendChild(newChild)
Add node newChild to the end of child list
replaceChild(newChild, oldChild)
Replace node oldChild with newChild
<html xmlns="http://www.w3.org/ insertBefore(newChild, oldChild)
Insert node newChild in child list before node oldChild
1999/xhtml"> cloneNode(deep)
<head><title>Navigating DOM</title> Return a copy of the node including all descendents if deep==true
<script type"text/javascript">... removeChild(aChild)
Remove node aChild from child list including all descendents
</script>
(all methods in Node are available to Document and Element)
</head> Document
<body><h1>A Nice Table</h1> createElement(tagName)
<table id="mTable“ border="1"> Create an Element object of type tagName
<tr><td>1</td><td>2</td></tr> createTextNode(data)
Create a Text object with data
</table><form><input
Element
type="button"onclick="showDOM()" removeAttribute(name)
value="Print DOM" /> setAttribute(name, value)
</form></body></html> http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
13 14
16
15
3
Event-Driven Execution Partial List of Events
JavaScript programs are typically event-driven Clipboard
Execution can be triggered by events that oncopy, oncut, onpaste
occur on the Web page, usually as a result of Keyboard
user actions
onkeydown, onkeyup, onkeypress
onclick,ondblclick, onkeydown, onload,
onmouseover, onsubmit, onresize, … Mouse
Events are associated with XHTML tag onmousedown, onmouseup, onmousemove
attributes Other
The onclick event can be associated with <a> onfocus, onblur, load, unload
and form <input> tags
19 20
21 22
4
Changing Style Attributes Example: Move Image on Screen
CSS1 and CSS-P (CSS - Positioning) are scriptable
from JavaScript
Allow dynamic style update on XHTML elements
The style property
position zIndex
top The effect of the 3rd dimension
left
The largest value is on the top
visibility
backgroundColor
color
25 26